tags:

views:

122

answers:

2

I'm new to Git and a bit confused. I have 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  A: 

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.

Htbaa
`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).
Chris Johnsen
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
+1  A: 

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.

Alex Brown
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.
Tom DeMille
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: http://www.kernel.org/pub/software/scm/git/docs/git-stash.html
seh
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
I think this is pretty bad advice. What use is a branch when you can just make another clone?!
Pod
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