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


当前回答

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

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

其他回答

你也可以传递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 branch -f branch-pointer-to-move new-pointer在TortoiseGit中:

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

git branch --force <branch-name> [<new-tip-commit>]

如果忽略new-tip-commit,则默认为当前提交。

New-tip-commit可以是一个分支名称(例如master, origin/master)。

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

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

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

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

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

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

例子:

吉特妙兰


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