Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git best practices for quickly switching between branches

Tags:

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?

like image 357
Richard Avatar asked Jun 16 '10 16:06

Richard


People also ask

What is the best branching strategy in git?

Build your strategy from these three concepts: Use feature branches for all new features and bug fixes. Merge feature branches into the main branch using pull requests. Keep a high quality, up-to-date main branch.

Which one is the best branching git workflow to follow?

Git Flow. The Git Flow is the most known workflow on this list. It was created by Vincent Driessen in 2010 and it is based in two main branches with infinite lifetime: master — this branch contains production code.


1 Answers

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.

like image 154
cweider Avatar answered Nov 24 '22 12:11

cweider