tags:

views:

86

answers:

4

I have multiple active branches that I need to work on at the same time. Clearly I can create two working directories with a distinct branch per directory. Is that the only way to do it without having to "commit" and "checkout" in order to switch from one branch to another?

+3  A: 

Yes, though you can use git stash instead of commit if you're not ready to finalize your current work in progress.

dahlbyk
I was unaware of this command. i like it because it's exactly what I was thinking. Just a place to put my stuff until I was really ready to commit. In the last week I have committed "snap" several times so that I could change branches. It made me very mad because many of those changes are likely to change again... In the meantime these changes will become public to my team when I push... some of which might be embarrassing. "stash" is to be tested ASAP. Thanks!
Richard
Glad to help. It's important to realize, as cweider noted, that you can always rewrite your local history as long as a commit hasn't been pushed. For example, if you have a "snap" commit between other commits you want to keep, you could `cherry-pick` it into a different branch, then do a `rebase -i` to remove it from the other branch.
dahlbyk
+2  A: 

git clone, through the local protocol, is a good alternative to be able to work on multiple branches at the same time.

I usually clone one local bare repo into multiple copies (one for each active branch), and use that bare repo as a central integration repo (since I can push easily to a bare repo, versus not being able to push to non-bare repo).

VonC
For the "bare repo" justification, see http://stackoverflow.com/questions/2041823/git-how-to-see-pulled-pushed-changes-in-origin/2041865#2041865 for instance
VonC
+2  A: 

If you are temporarily switching branches git stash is useful, however, remember that commits don’t need to persist forever; you may make temporary commits to roll back later.

So my recommendation is, if it is a many hours long switch, to do a git commit instead, because, depending on your memory, stashes can be easy to forget/lose/etc.

[In MyBranch]
>$ git commit -m "WIP: Stuff I was working on."
>$ git checkout AnotherBranch
[Do Stuff]
>$ git checkout MyBranch
>$ git reset HEAD^
[Continue]

And since this is a question about best practices, remember to give your stash a useful message using git stash save otherwise it can be difficult to find later.

cweider
+1  A: 

If you are doing what is called branch-per-feature development as explained here:

http://martinfowler.com/bliki/FeatureBranch.html

you might want to also ensure that you switch the database schemas. Git can help in this by means of smudge and clean. Managing multiple databases locally is then possible. When you checkout a new branch, you smudge the connection string to annotate the database name with the branch name. Should the configuration file be committed at any point, it is cleaned by removing the name of the branch from the connection.

For more information, take a look at the Pro Git book:

http://progit.org/book/ch7-2.html

HTH,

Adam

adymitruk