0

I am trying to do some stuff using git.

I had the following commands to fetch and to "clean" my current branch and to make it (my current branch) exactly the same as the remote branch origin/master :

  git fetch
  git reset --hard origin/master

This is working.

Now I'd like to make my current branch exactly the same as remote branch named myremotebranch. This is, instead of making my current branch the same as origin/master, I'd like to do my current branch the same as myremotebranch (remote branch).

By doing:

git fetch
git reset --hard origin/myremotebranch

Can I accomplish this? (make my current local branch just like remote remotebranch?

H.N.
  • 1,207
  • 2
  • 12
  • 28
  • Yep, should work – eftshift0 May 26 '22 at 22:42
  • I guess If after this If I want to switch my current local branch to e like remote master agian I can do again: git fetch and git reset --hard origin/master correct? – H.N. May 26 '22 at 22:47
  • I would wonder why you are resetting --hard the branch so often (like, what are you trying to achieve?) but fact of the matter is that you can do it as many times as you like. – eftshift0 May 26 '22 at 22:53
  • I have some stuff working in master. But I am doing some tests using a dummy branch: myremotebranch. I don't want to mess with my remote master :) so when I push something into myremotebranch, I have a script which I want to pull the latest myremotebranch into local branch – H.N. May 26 '22 at 22:57
  • I think you my like the [branch agnostic syntax](https://stackoverflow.com/a/61490618/184546), at least for some of your resets. – TTT May 27 '22 at 02:42
  • 2
    Consider not using branches at all. *Git* doesn't need them, and if your intent is to test one specific commit, locally, you can just check out that one specific commit, locally, as a "detached HEAD". Branch names exist to make your (human) work easier. If they are making your work *harder* (than not using them), don't use them: just `git switch --detach origin/master` to check out that commit and use it for a bit, then `git switch --detach origin/myremotebranch` to use that commit for a while. To make *new* commits, branch names will make your life easier, so use them *then*. – torek May 27 '22 at 05:19
  • @torek beat me to it by a few minutes. Consider checking out the remote branch if all you want to do is 'take a look': `git checkout some-remote/some-branch` – eftshift0 May 27 '22 at 05:46

1 Answers1

0

Yes, your reset commands will work as you describe. But you don't need to have a local branch to move to a different commit. You can check out commits directly:

git checkout origin/master
git checkout origin/myremotebranch

This will get you into detached HEAD state. No need to have a branch to be reset all the time.

knittl
  • 246,190
  • 53
  • 318
  • 364