问题
To move the branch pointer of a checked out branch, one can use the git reset --hard command. But how to move the branch pointer of a not-checked out branch to point at a different commit (keeping all other stuff like tracked remote branch)?
回答1:
N.B. If you simply want to move a branch to another commit, the easiest way is
git branch -f branch-name new-tip-commitas detailed by Chris Johnsen's answer.
You can do it for arbitrary refs. This is how to move a branch pointer:
git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit>
The general form:
git update-ref -m "reset: Reset <branch> to <new commit>" <ref> <commit>
You can pick nits about the reflog message if you like - I believe the branch -f one is different from the reset --hard one, and this isn't exactly either of them.
回答2:
git branch -f <branch-name> <new-tip-commit>
回答3:
You can also pass git reset --hard a commit reference.
For example:
git checkout branch-name
git reset --hard new-tip-commit
I find I do something like this semi-frequently:
Assuming this history
$ git log --decorate --oneline --graph
* 3daed46 (HEAD, master) New thing I shouldn't have committed to master
* a0d9687 This is the commit that I actually want to be master
# Backup my latest commit to a wip branch
$ git branch wip_doing_stuff
# Ditch that commit on this branch
$ git reset --hard HEAD^
# Now my changes are in a new branch
$ git log --decorate --oneline --graph
* 3daed46 (wip_doing_stuff) New thing I shouldn't have committed to master
* a0d9687 (HEAD, master) This is the commit that I actually want to be master
回答4:
Just to enrich the discussion, if you want to move myBranch branch to your current commit, just omit the second argument after -f
Example:
git branch -f myBranch
I generally do this when I rebase while in a Detached HEAD state :)
回答5:
In gitk --all:
- right click on the commit you want
- -> create new branch
- enter the name of an existing branch
- press return on the dialog that confirms replacing the old branch of that name.
Beware that re-creating instead of modifying the existing branch will lose tracking-branch information. (This is generally not a problem for simple use-cases where there's only one remote and your local branch has the same name as the corresponding branch in the remote. See comments for more details, thanks @mbdevpl for pointing out this downside.)
It would be cool if gitk had a feature where the dialog box had 3 options: overwrite, modify existing, or cancel.
Even if you're normally a command-line junkie like myself, git gui and gitk are quite nicely designed for the subset of git usage they allow. I highly recommend using them for what they're good at (i.e. selectively staging hunks into/out of the index in git gui, and also just committing. (ctrl-s to add a signed-off: line, ctrl-enter to commit.)
gitk is great for keeping track of a few branches while you sort out your changes into a nice patch series to submit upstream, or anything else where you need to keep track of what you're in the middle of with multiple branches.
I don't even have a graphical file browser open, but I love gitk/git gui.
回答6:
The recommended solution git branch -f branch-pointer-to-move new-pointer in TortoiseGit:
- "Git Show log"
- Check "All Branches"
- On the line you want the branch pointer to move to (new-pointer):
- Right click, "Create Branch at this version"
- Beside "Branch", enter the name of the branch to move (branch-pointer-to-move)
- Under "Base On", check that the new pointer is correct
- Check "Force"
- Ok
回答7:
Honestly, I'm surprised how nobody thought about the git push command:
git push -f . <destination>:<branch>
The dot ( . ) refers the local repository, and you may need the -f option because the destination could be "behind its remote counterpart".
Although this command is used to save your changes in your server, the result is exactly the same as if moving the remote branch (<branch>) to the same commit as the local branch (<destination>)
回答8:
In the case the commit you want to point to is ahead of the current branch (which should be the case unless you want to undo the last commits of the current branch), you can simply do:
git merge <commit>
来源:https://stackoverflow.com/questions/5471174/move-branch-pointer-to-different-commit-without-checkout