Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On local branch, don't want to commit changes, but need to switch to another branch

I am working on branch A and the feature/task is not done. Then I need to switch to another branch B for a quick fix. When I try to switch on another branch Git forces me to save my local changes otherwise I 'll lose my all local changes.

I need to commit my incomplete code. Is there any way that I can switch between multiple branches without committing and losing any code? Or is there a better way to handle the situation?

like image 485
Suleman Ahmad Avatar asked Aug 06 '13 06:08

Suleman Ahmad


1 Answers

You can use git stash, which will save your changes without creating a commit.1

First, stash your changes:

$ git stash

Then switch to your other branch:

$ git checkout branch-B

When you're ready, go back to your original branch and unstash your changes:

$ git checkout branch-A
$ git stash pop

See the documentation linked above for more details and specifics on additional use cases.


1 Technically it does create a commit, but git stash uses some magic so you don't actually see the commit, and Git's tools know how to deal properly with these pseudo-commits.

like image 100
mipadi Avatar answered Sep 30 '22 17:09

mipadi