git branch -f <branch-name> <new-tip-commit>
注意:如果您只是想将分支移至另一个提交,最简单的方法是
git branch -f branch-name new-tip-commit
您可以为任意引用执行此操作。这是移动分支指针的方法:
git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit>
一般形式:
git update-ref -m "reset: Reset <branch> to <new commit>" <ref> <commit>
如果愿意,您可以选择关于 reflog 消息的内容 - 我相信branch -f
一个与reset --hard
一个不同,但这不完全是其中之一。
您还可以通过git reset --hard
提交参考。
例如:
git checkout branch-name
git reset --hard new-tip-commit
我发现我经常做这样的事情:
假设这个历史
$ 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