'git - move branch "tag" to other commit?
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
Solution 1:[1]
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.
Solution 2:[2]
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
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | J.beenie |
| Solution 2 | Van Coding |
| Solution 3 | martin.zaenker |
