在不接触工作树的情况下切换分支?
我目前位于调试分支上,并且想切换到主分支,而不修改工作树(保留其在调试分支中的方式),因此我可以将一些更改提交到主分支中。
有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以执行以下操作:
说明:
如果您位于
debug
分支并执行git reset --soft master
您将保持工作树和索引不变并移至master 指向的提交。问题是,debug
也将重置为此提交。因此,您对debug
的提交“丢失”了(好吧,不是真的,但它们不再可以直接访问),并且您仍然位于debug
分支上。为了防止
git reset
移动debug
但仍将HEAD
设置为master
提交,您首先执行git checkout --detach
将HEAD
直接指向当前提交(请参阅man git-checkout
,“分离头”部分)。然后您可以在不接触debug
分支的情况下进行重置。现在
HEAD
直接指向master
指向的提交,即它仍然是分离的。您只需git checkout master
即可附加到master
,现在就可以在master
分支上提交。请注意,git checkout(默认情况下且未传递路径时)仅更新“源”和“目标”提交之间已更改的文件,并保留对工作树中文件的本地修改。由于在这种情况下两次提交是相同的,因此工作目录中的任何文件都不会被触及。
You can do the following:
Explanation:
If you are on the
debug
branch and would dogit reset --soft master
you would leave your working tree and index untouched and move to the commitmaster
points to. The problem is,debug
will be reset to this commit too. So your commits ondebug
are "lost" (well, not really, but they are not directly accessible anymore) and you are still on thedebug
branch.To prevent
git reset
from movingdebug
but still setting yourHEAD
to themaster
commit, you first dogit checkout --detach
to pointHEAD
directly to your current commit (seeman git-checkout
, section "DETACHED HEAD"). Then you can do the reset without touching thedebug
branch.Now
HEAD
is pointing directly to the commitmaster
points to, i.e. it is still detached. You can simplygit checkout master
to attach tomaster
and are now ready to commit on themaster
branch.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.您可以将头重置为指向 master,而不更改索引或工作树:
您可能应该重置索引,以便可以有选择地应用工作树更改,否则您最终可能会提交 master 和调试分支之间的所有差异,这可能是一件坏事。
完成所需的提交后,您可以使用以下命令返回到调试分支:
You can reset your head to point at master without changing the index or working tree with:
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.
Once you've made the commit that you want to make you can return to your debug branch with:
您可以存储(
git stash
)您的更改、切换分支、取消存储(git stash pop
)您的更改、添加并提交更改。如果您想要调试的确切状态,只需将调试合并到主控中(或重置主控以进行调试)。
You can stash (
git stash
) your changes, switch branches, unstash (git stash pop
) your changes, add and commit the changes.If you want the exact state of debug, then simply merge debug into master (or reset master to debug).
这是一个原始工作流程
,要返回
,您也可以在“此处”提交相关更改,然后将/cherry-pick 推送到主分支上。
Here is a raw workflow
And to go back
Alternatively, you might just commit the relevant changes 'here' and push / cherry-pick onto the master branch.
据我所知,你不能。分支切换意味着将位于该分支 HEAD 中的代码版本签入工作副本。
您想要
合并
您的分支。现在分支将被同步。如果要合并更改的子集,可以指定一个提交或一系列提交。
另请查看 cherry-pick
例如:
将 devel 中的最后一次提交合并回 master。
如果您需要合并位于不同主机上的两个分支,请查看 git pull 和 git push。
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.
You want to
merge
your branches. DoThe 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-pick
For example:
Will merge the last commit in devel back into master.
If you need to merge two branches that sit on different hosts, have a look at git pull and git push.