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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:30:22  来源:igfitidea点击:

Switching branches without touching the working tree?

gitversion-controlbranch

提问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 debugbranch and would do git reset --soft masteryou would leave your working tree and index untouched and move to the commit masterpoints to. The problem is, debugwill be reset to this commit too. So your commits on debugare "lost" (well, not really, but they are not directly accessible anymore) and you are still on the debugbranch.

如果您在debug分支上并且愿意这样做,git reset --soft master您将保持工作树和索引不变并移至提交master点。问题是,debug也将重置为此提交。所以你的提交debug“丢失了”(好吧,不是真的,但它们不再直接访问了)并且你仍然在debug分支上。

To prevent git resetfrom moving debugbut still setting your HEADto the mastercommit, you first do git checkout --detachto point HEADdirectly to your current commit (see man git-checkout, section "DETACHED HEAD"). Then you can do the reset without touching the debugbranch.

为了防止git reset移动debug但仍将您HEADmaster提交设置为提交,您首先git checkout --detachHEAD直接指向您当前的提交(请参阅man git-checkout“DETACHED HEAD”部分)。然后你可以在不接触debug分支的情况下进行重置。

Now HEADis pointing directly to the commit masterpoints to, i.e. it is still detached. You can simply git checkout masterto attach to masterand are now ready to commit on the masterbranch.

现在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 mergeyour 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。