Switching branches without touching the working tree?

亡梦爱人 提交于 2019-11-28 16:26:13

问题


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?


回答1:


This answer uses low-level "plumbing" commands. Be careful. If you prefer "porcelain" commands, go with this answer which produces the same results.

You can reset your head to point at master without changing the index or working tree with:

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.

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



回答2:


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.

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 debugbranch.

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.




回答3:


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).




回答4:


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.




回答5:


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. Do

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-pick For example:

git checkout master
git cherry-pick devel

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.



来源:https://stackoverflow.com/questions/6070179/switching-branches-without-touching-the-working-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!