18

I'm new to Git and a bit confused. I have a Master branch and have created a second feature branch.

If I make changes in my feature branch and then switch to Master, will my changes be lost if I don't commit?

Where does stash come into play, is it something you do before you switch branches (but don't want to commit) or is it to simply revert some changes so you can get back to previous code temporarily?

2 Answers 2

15

You can't change to another branch unless you clean your tree. This is done by committing your changes, reverting them or saving them to the stash.

2
  • 5
    git checkout will ‘carry forward’ both staged and unstaged changes when switching branches as long the changes are limited to files that are otherwise identical in the current branch and the new branch (i.e. there is no chance of conflicts if the changes were to be applied to either branch). Additionally, one can force a merge attempt by using git checkout --merge, but this may result in merge conflicts and those conflicts may be difficult to resolve (which may make it difficult to return to the pre-checkout state). Mar 20, 2010 at 5:54
  • Wow don't know who it was but thanks for the -2 downvote... I figured this one out a while ago as well but at the time of writing I remember it wasn't possible to do so. Is it new for Git 1.7 ?
    – Htbaa
    Mar 22, 2010 at 9:49
1

You probably don't want to use stash for this purpose.

If you really want to be developing on the master and a feature branch at the same time, I suggest cloning your repository, working in the clone and the master, and then using push and pull to move changes between them.

If you are switching frequently between them, I suggest checking in before you switch; there's nothing wrong with checking in garbage; git makes it easy to sort this out later on.

6
  • here is my situation. I have a solution with a few websites. I would like to work on different versions of the project at the same time, I don't want to have to use different working directories for each one. So I'm looking for a way to switch between various versions of the project but have git manage changing all the files around for me so I can use one IIS setup pointed to each web site, not have to change all my settings that point to specific paths, etc. Jan 8, 2010 at 22:41
  • 1
    Note that stash works by checking in just as you suggest. It's a valid commit in the repository just like any other. It happens to not be referenced from a branch ref, but it's pinned by the stash ref. See the DISCUSSION section here: kernel.org/pub/software/scm/git/docs/git-stash.html
    – seh
    Jan 8, 2010 at 22:55
  • 3
    ah, but it's easy to lose track of the fact that you have stashed, if you attention has moved on to other things. In my opinion, it's safer to commit to the head of your branch.
    – Alex Brown
    Jan 8, 2010 at 23:06
  • 1
    I think this is pretty bad advice. What use is a branch when you can just make another clone?!
    – Pod
    Jan 14, 2010 at 15:34
  • I don't understand your comment; can you expand? In my answer I recommend using clone, but if the user is keen on using a single repository I advised against using stash.
    – Alex Brown
    Jan 14, 2010 at 15:59

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.