在不接触工作树的情况下切换分支? [英] Switching branches without touching the working tree?
问题描述
我目前在调试分支上,并且想切换到主分支,而不修改工作树(保持它在调试分支中的方式),所以我可以将一些更改提交到主分支分支.
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?
推荐答案
您可以执行以下操作:
git checkout --detach
git reset --soft master
git checkout master
<小时>
说明:
如果你在 debug
分支上并且会执行 git reset --soft master
你会保持你的工作树和索引不变并移动到提交 master
指向.问题是,debug
也将被重置为此提交.所以你在 debug
上的提交丢失了"(嗯,不是真的,但它们不再直接访问了),你仍然在 debug
分支上.
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.
为了防止 git reset
移动 debug
但仍将您的 HEAD
设置为 master
提交,您首先执行 git checkout --detach
以将 HEAD
直接指向您当前的提交(请参阅 man git-checkout
,DETACHED HEAD"部分).然后你可以在不接触 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.
现在 HEAD
直接指向 master
指向的提交,即它仍然是分离的.您可以简单地将 git checkout master
附加到 master
,现在就可以在 master
分支上提交了.
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.
请注意,git checkout
(默认情况下且未传递路径时)仅更新在源"和目标"提交之间更改的文件以及对工作中文件的本地修改树被保留.由于在这种情况下两次提交是相同的,因此不会触及工作目录中的任何文件.
Note that git checkout
(by default and when no path is passed) only updates files that have been changed between the "source" and "target" commit and local modifications to the files in the working tree are kept. As both commits are the same in this case, no files in the working directory are touched.
这篇关于在不接触工作树的情况下切换分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!