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


当前回答

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

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 -难的,这不是他们中的任何一个。

其他回答

为了丰富讨论内容,如果你想将myBranch分支移动到当前提交,只需省略-f之后的第二个参数

例子:

吉特妙兰


我通常这样做,当我在一个分离的头部状态下重新设置:)

Git 2.23.0引入了Git -switch命令,该命令也可以用于完成此任务。

git switch -C <branch-name> [<start-point>]

-C(大写C)选项表示如果<branch-name>已经存在,则将其重置为<起始点>。

使用-c(小写C),它将尝试创建一个新的分支,但如果已经存在则会失败。

<start-point>可以是一个散列、一个标记或另一个分支名称。

你也可以传递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 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 -难的,这不是他们中的任何一个。

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

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