Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch to another branch without committing

Tags:

git

I am working on a feature branch and have not finished the work there - Now I need to change to a different branch to fix something

for example

feat1 - 6+ files changed

When I checkout to feat2 branch, after git add . in feat1, git seems to carry over the staged yet uncommitted file changes.

If I commit these file changes in feat1, checking out to feat2 will not carry over those changes

How can I switch branches without committing file changes?

like image 404
Nick Ginanto Avatar asked Feb 02 '14 20:02

Nick Ginanto


People also ask

Can you switch branches without committing changes?

Warning You should commit all of your current changes before switching branches. If you have uncommitted changes when you switch branches, they will be lost.

What happens to uncommitted changes when you switch branches?

If you have modifications to a file that is identical between two branches, switch from one branch to the other will not require a stash. If the file is different on your other branch, Git will not let you switch branches, as this would destroy your uncomitted changes.


1 Answers

Stash them:

$ git stash save -u "Some logical description of the changes"
$ git stash list
stash@{0}: Some logical description of the changes
$ git checkout other-branch

When you're done, you can use git stash apply to apply the changes in your stash and keep the stash around, or git stash pop to apply the changes and remove the stash as long as there are no conflicts.

If you end up with multiple stashes at once you can apply or pop them in an arbitrary order using the stash@ string, e.g. git stash apply stash@{6}.

like image 194
Chris Avatar answered Oct 19 '22 10:10

Chris