Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 594
static_rtti Avatar asked May 20 '11 09:05

static_rtti


People also ask

Can I switch branches without committing?

when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.

What happens when you switch branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.


2 Answers

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.

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.

like image 167
siegi Avatar answered Sep 29 '22 12:09

siegi


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 
like image 34
CB Bailey Avatar answered Sep 29 '22 13:09

CB Bailey