要移动检出分支的分支指针,可以使用git reset——hard命令。但是如何移动未签出分支的分支指针指向不同的提交(保持所有其他东西,如跟踪远程分支)?


当前回答

对于检出的分支,在你想要指向的提交在当前分支之前的情况下(除非你想撤销当前分支的最后一次提交,否则应该是这样),你可以简单地做:

git merge --ff-only <commit>

这是一个较软的git重置的替代方案——很难,如果你不是在上面描述的情况下,就会失败。

对一个未签出的分支做同样的事情,等价的是:

git push . <commit>:<branch>

其他回答

建议解决方案git branch -f branch-pointer-to-move new-pointer在TortoiseGit中:

“Git显示日志” 检查“所有分支” 在你想要分支指针移动到的那一行(new-pointer): 右键单击“在此版本创建分支” 在“Branch”旁边输入要移动的分支名称(Branch -pointer- move) 在“Base On”下,检查新指针是否正确 检查“力” 好吧

你也可以传递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

对于检出的分支,在你想要指向的提交在当前分支之前的情况下(除非你想撤销当前分支的最后一次提交,否则应该是这样),你可以简单地做:

git merge --ff-only <commit>

这是一个较软的git重置的替代方案——很难,如果你不是在上面描述的情况下,就会失败。

对一个未签出的分支做同样的事情,等价的是:

git push . <commit>:<branch>

打开文件.git/refs/heads/<your_branch_name>,并将存储在那里的散列更改为您想要移动分支头部的散列。只需编辑和保存文件与任何文本编辑器。只需确保要修改的分支不是当前活动的分支。

免责声明:可能不是一个明智的方法,但可以完成工作。

你可以对任意裁判这样做。这是如何移动分支指针:

git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit>

其中-m为分支的reflog添加一条消息。

一般形式是

git update-ref -m "reset: Reset <branch> to <new commit>" <ref> <commit>

如果你喜欢,你可以挑关于reflog消息的虱子-我相信branch -f是不同于reset -难的,这不是他们中的任何一个。