git 在不接触工作树的情况下切换分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6070179/
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
Switching branches without touching the working tree?
提问by static_rtti
I am currently on a debug branch, and would like to switch to the master branch, without modifying the working tree (leave it the way it is in the debug branch), so I can commit some of the changes into the master branch.
我目前在调试分支上,并且想切换到主分支,而不修改工作树(保持它在调试分支中的方式),所以我可以将一些更改提交到主分支。
Is there a way to do this?
有没有办法做到这一点?
回答by siegi
You can do the following:
您可以执行以下操作:
git checkout --detach
git reset --soft master
git checkout master
Explanation:
解释:
If you are on the debug
branch and would do git reset --soft master
you would leave your working tree and index untouched and move to the commit master
points to. The problem is, debug
will be reset to this commit too. So your commits on debug
are "lost" (well, not really, but they are not directly accessible anymore) and you are still on the debug
branch.
如果您在debug
分支上并且愿意这样做,git reset --soft master
您将保持工作树和索引不变并移至提交master
点。问题是,debug
也将重置为此提交。所以你的提交debug
“丢失了”(好吧,不是真的,但它们不再直接访问了)并且你仍然在debug
分支上。
To prevent git reset
from moving debug
but still setting your HEAD
to the master
commit, you first do git checkout --detach
to point HEAD
directly to your current commit (see man git-checkout
, section "DETACHED HEAD"). Then you can do the reset without touching the debug
branch.
为了防止git reset
移动debug
但仍将您HEAD
的master
提交设置为提交,您首先git checkout --detach
要HEAD
直接指向您当前的提交(请参阅man git-checkout
“DETACHED HEAD”部分)。然后你可以在不接触debug
分支的情况下进行重置。
Now HEAD
is pointing directly to the commit master
points to, i.e. it is still detached. You can simply git checkout master
to attach to master
and are now ready to commit on the master
branch.
现在HEAD
直接指向提交master
点,即它仍然是分离的。您可以简单git checkout master
地附加到分支master
并准备好在master
分支上提交。
回答by CB Bailey
This answer uses low-level "plumbing" commands. Be careful. If you prefer "porcelain" commands, go with this answerwhich produces the same results.
You can reset your head to point at master without changing the index or working tree with:
您可以在不更改索引或工作树的情况下将头部重置为指向 master:
git symbolic-ref HEAD refs/heads/master
You should probably reset the index so that you can selectively apply your working tree changes, otherwise you may end up committing all the differences between master and the debug branch, which is probably a bad thing.
您可能应该重置索引,以便您可以有选择地应用您的工作树更改,否则您最终可能会提交 master 和 debug 分支之间的所有差异,这可能是一件坏事。
git reset
Once you've made the commit that you want to make you can return to your debug branch with:
完成您想要的提交后,您可以使用以下命令返回调试分支:
git symbolic-ref HEAD refs/heads/debug-branch
git reset
回答by knittl
You can stash (git stash
) your changes, switch branches, unstash (git stash pop
) your changes, add and commit the changes.
您可以存储 ( git stash
) 您的更改、切换分支、取消存储 ( git stash pop
) 您的更改、添加和提交更改。
If you want the exact state of debug, then simply merge debug into master (or reset master to debug).
如果你想要调试的确切状态,那么只需将调试合并到主(或重置主调试)。
回答by sehe
Here is a raw workflow
这是一个原始的工作流程
git stash
git checkout otherbranch
git stash apply
git reset
git add # interactively? just add the hunks/changes you want to commit
git commit
And to go back
然后回去
git reset --hard # watch it here! make sure you haven't added more changes that you wanted to keep
git checkout debug
git stash pop
Alternatively, you might just commit the relevant changes 'here' and push / cherry-pick onto the master branch.
或者,您可以只在“此处”提交相关更改,然后将 /cherry-pick 推送到主分支。
回答by VladFr
As far as I know, you can't. Branch switching means checking out into the working copy a version of the code that sits in the HEAD of that branch.
据我所知,你不能。分支切换意味着将位于该分支 HEAD 中的代码版本检出到工作副本中。
You want to merge
your branches. Do
你想merge
你的分支机构。做
git checkout master
git merge devel
The branches will now be synchronized. If you want to merge a subset of changes, you can specify a commit or a range of commits. Also take a look at cherry-pickFor example:
现在将同步分支。如果要合并更改的子集,可以指定提交或提交范围。也看看cherry-pick例如:
git checkout master
git cherry-pick devel
Will merge the last commit in devel back into master.
将 devel 中的最后一次提交合并回 master。
If you need to merge two branches that sit on different hosts, have a look at git pull and git push.
如果您需要合并位于不同主机上的两个分支,请查看 git pull 和 git push。