1

I have checked out a particular branch from my master. I created new classes propery files etc. in my local Eclipse and did the testing. I have not added/committed those classes/files to my local git repo. So after this successful prototype testing if I want to switch to another branch, how can I do it without losing local changes.

Once I check out the second branch my local changes will be overwritten? I am not sure if my changes will have to be discarded before the checkout of second branch to be possible.

1
  • if you are using source tree, stash the changes give it a name, switch to other branch, and when you comeback, re apply the stash to get your changes back Feb 11, 2016 at 19:36

2 Answers 2

3

Use git stash.

git-stash - Stash the changes in a dirty working directory away

See more in the git docs.

If you have untracked new files, be sure to use the -u flag, as includes files git hasn't tracked yet.

Once you're ready to bring your stashed changes back, use git stash pop.

$ git stash -u
$ git checkout <branch>
$ # do work
$ git checkout <oldbranch>
$ git stash pop
0

Well it is not certain you would lose changes (and in fact if you were to, git checkout would warn you), but it would be best to either stash your changes with git stash or just go ahead and commit your changes before checking out the other branch.

2
  • Thanks! So my chsnges will always remain associated with the old branch, correct? I cannot stash them away, and bring them back in a new branch.
    – user721025
    Feb 12, 2016 at 0:03
  • No, if you wish to stash them and apply the stash in a new branch, you can totally do that. Feb 12, 2016 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.