git 无需结帐即可将分支指针移动到不同的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5471174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move branch pointer to different commit without checkout
提问by Mot
To move the branch pointer of a checked out branch, one can use the git reset --hard
command. But how to move the branch pointer of a not-checked out branch to point at a different commit (keeping all other stuff like tracked remote branch)?
要移动已检出分支的分支指针,可以使用该git reset --hard
命令。但是如何将未签出分支的分支指针移动到不同的提交(保留所有其他内容,如跟踪的远程分支)?
采纳答案by Adam A
You can do it for arbitrary refs. This is how to move a branch pointer:
您可以为任意参考执行此操作。这是移动分支指针的方法:
git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit>
The general form:
一般形式:
git update-ref -m "reset: Reset <branch> to <new commit>" <ref> <commit>
You can pick nits about the reflog message if you like - I believe the branch -f
one is different from the reset --hard
one, and this isn't exactly either of them.
如果你愿意,你可以选择关于 reflog 消息的 nits - 我相信那个branch -f
与那个不同reset --hard
,这不完全是其中的任何一个。
回答by Chris Johnsen
git branch -f <branch-name> <new-tip-commit>
回答by Amiel Martin
You can also pass git reset --hard
a commit reference.
您还可以传递git reset --hard
提交引用。
For example:
例如:
git checkout branch-name
git reset --hard new-tip-commit
I find I do something like this semi-frequently:
我发现我经常做这样的事情:
Assuming this history
假设这段历史
$ 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
回答by Matheus Felipe
Just to enrich the discussion, if you want to move myBranch
branch to your currentcommit, just omit the second argument after -f
只是为了丰富讨论,如果您想将myBranch
分支移动到当前提交,只需省略后面的第二个参数-f
Example:
例子:
git branch -f myBranch
git branch -f myBranch
I generally do this when I rebase
while in a Detached HEAD state :)
当我rebase
处于 Detached HEAD 状态时,我通常会这样做:)
回答by Peter Cordes
In gitk --all
:
在gitk --all
:
- right click on the commit you want
- -> create new branch
- enter the name of an existing branch
- press return on the dialog that confirms replacing the old branch of that name.
- 右键单击您想要的提交
- ->创建新分支
- 输入现有分支的名称
- 在确认替换该名称的旧分支的对话框上按回车键。
Beware that re-creating instead of modifying the existing branch will lose tracking-branch information. (This is generally not a problem for simple use-cases where there's only one remote and your local branch has the same name as the corresponding branch in the remote. See comments for more details, thanks @mbdevpl for pointing out this downside.)
请注意,重新创建而不是修改现有分支将丢失跟踪分支信息。(对于只有一个远程并且您的本地分支与远程中的相应分支具有相同名称的简单用例,这通常不是问题。有关更多详细信息,请参阅评论,感谢@mbdevpl 指出这一缺点。)
It would be cool if gitk
had a feature where the dialog box had 3 options: overwrite, modify existing, or cancel.
如果gitk
对话框有 3 个选项:覆盖、修改现有或取消,那就太好了。
Even if you're normally a command-line junkie like myself, git gui
and gitk
are quite nicely designed for the subset of git usage they allow. I highly recommend using them for what they're good at (i.e. selectively staging hunks into/out of the index in git gui, and also just committing. (ctrl-s to add a signed-off: line, ctrl-enter to commit.)
即使您通常是像我这样的命令行迷,git gui
并且gitk
非常适合他们允许的 git 使用子集。我强烈建议将它们用于他们擅长的地方(即有选择地将大块块放入/取出 git gui 中的索引,并且也只是提交。(ctrl-s 添加签名:行,ctrl-enter 提交) .)
gitk
is great for keeping track of a few branches while you sort out your changes into a nice patch series to submit upstream, or anything else where you need to keep track of what you're in the middle of with multiple branches.
gitk
非常适合跟踪几个分支,同时将更改整理成一个不错的补丁系列以提交上游,或者其他任何需要跟踪多个分支中间的内容的地方。
I don't even have a graphical file browser open, but I love gitk/git gui.
我什至没有打开图形文件浏览器,但我喜欢 gitk/git gui。
回答by ax.
The recommended solution git branch -f branch-pointer-to-move new-pointer
in TortoiseGit:
该建议的解决方法git branch -f branch-pointer-to-move new-pointer
在TortoiseGit:
- "Git Show log"
- Check "All Branches"
- On the line you want the branch pointer to move to (new-pointer):
- Right click, "Create Branch at this version"
- Beside "Branch", enter the name of the branch to move (branch-pointer-to-move)
- Under "Base On", check that the new pointer is correct
- Check "Force"
- Ok
- “Git 显示日志”
- 勾选“所有分行”
- 在您希望分支指针移动到(新指针)的行上:
- 右键单击“在此版本上创建分支”
- 在“分支”旁边,输入要移动的分支名称(branch-pointer-to-move)
- 在“Base On”下,检查新指针是否正确
- 勾选“强制”
- 好的
回答by Adrian
Honestly, I'm surprised how nobody thought about the git push
command:
老实说,我很惊讶没有人想到这个git push
命令:
git push -f . <destination>:<branch>
The dot ( . ) refers the local repository, and you may need the -f option because the destination could be "behind its remote counterpart".
点 ( . ) 指的是本地存储库,您可能需要 -f 选项,因为目标可能“在其远程副本之后”。
Although this command is used to save your changes in your server, the result is exactly the same as if moving the remote branch (<branch>
) to the same commit as the local branch (<destination>
)
尽管此命令用于将更改保存在服务器中,但结果与将远程分支 ( <branch>
)移动到与本地分支 ( <destination>
)相同的提交完全相同
回答by Guillermo Gutiérrez
Open the file .git/refs/heads/<your_branch_name>
, and change the hash stored there to the one where you want to move the head of your branch. Just edit and save the file with any text editor. Just make sure that the branch to modify is not the current active one.
打开文件.git/refs/heads/<your_branch_name>
,并将存储在那里的哈希更改为您想要移动分支头部的哈希。只需使用任何文本编辑器编辑并保存文件。只要确保要修改的分支不是当前活动的分支。
Disclaimer:Probably not an advisable way to do it, but gets the job done.
免责声明:可能不是一种可取的方法,但可以完成工作。
回答by Jean Paul
In the case the commit you want to point to is ahead of the current branch (which should be the case unless you want to undo the last commits of the current branch), you can simply do:
如果您要指向的提交位于当前分支之前(除非您想撤消当前分支的最后一次提交,否则应该是这种情况),您可以简单地执行以下操作:
git merge <commit>