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?
Yes, though you can use git stash
instead of commit
if you're not ready to finalize your current work in progress.
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).
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.
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