8

Since a branch is more or less only a tag, that moves automatically to the new commit, I wonder if I can modify this "tag".

Example:

             master  
A -- B -- C -- D

git checkout master would be the same as git checkout D

Can I change master to point to commit B?

   master
A -- B -- C -- D

git checkout master would now be the same as git checkout B

Use Case

Imagine someone has pushed one single commit to the online repository. When I do git fetch, I get this commit local, but my master branch still points to the commit before, while origin/master points to the new commit. I just want to move the local master branch to the same commit as origin/master points to.

So, I wouldn't have to merge.

Thanks for your help

1
  • In your push scenario, you would simply merge origin/master. Since you don't have any local commits, it will be resolved as a farst-forward
    – knittl
    Mar 2, 2012 at 16:46

4 Answers 4

12

To move the branch Tag to commit B you can do the following:

git branch -f master B

Using git branch instead of git reset --hard even preserves your working directory.

3
  • 2
    Looks like this would be right, but you reset it to where it already is using this command. I assume you're going for: git branch -f master B.
    – Matt D
    Mar 28, 2012 at 19:36
  • 1
    Does this preserve the branches tracking status? Jul 19, 2013 at 16:10
  • I'm not sure, but I'd guess no.
    – knittl
    Jul 21, 2013 at 17:14
2

This should work:

git reset --hard origin/master
0

I found another solution to this:

git fetch
git checkout origin/master
git branch -d master
git branch master

It's more logical to me

1
  • Note that you will be in detached head state after your sequence of commands.
    – knittl
    Mar 3, 2012 at 18:22
0

An annotation to knittis solution:

After calling to recreate the branchname(master) to point to the commit

git branch -f branchname D

Don't forget to synchronize HEAD and branchname(master) via:

git checkout branchname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.