要移动检出分支的分支指针,可以使用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”下,检查新指针是否正确 检查“力” 好吧
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式