Rajesh Pantula
Rajesh Pantula

Reputation: 10241

Switch a branch and preserve current branch changes in Git

How do I switch the branch and still preserve my current branch changes?

Consider this scenario.

I am working on an issue and as per our development model, I create a separate branch and start working on it. I created my new branch using

git checkout -b new_branch

Now it works fine when I do any changes in this branch.

But after this I forgot and switched back to default local branch(say "local"), due to system restart.

Now I started working on my fixes and suddenly realize that I am in "local" branch. Now because I have to commit my changes and merge it with master.

I am trying to do

git checkout new_branch

But I keep getting this error which says :

error: Your local changes to the following files would be overwritten by checkout: content/some/blah/blah/path_name/file.jsp

Please, commit your changes or stash them before you can switch branches. Aborting

How can I switch to the new_branch and still preserve my changes in local branch which I want to actually commit in new_branch.

PS :- stashing is not an option. Neither is git checkout on local branch. I want to preserve the changes.

Upvotes: 3

Views: 2106

Answers (1)

Chris Hayes
Chris Hayes

Reputation: 12050

Just stash the changes temporarily and immediately pop them right after switching branches.

git stash
git checkout new_branch
git stash pop

Upvotes: 12

Related Questions