A simple everyday workflow with Git

A great thing about Git is that it is flexible enough for you to develop your own favourite workflow, rather than have it dictate to you which workflow you should use. Personally, I’ve spent quite some time finding a nice way to apply Git in my day to day workflow. The resulting workflow is pretty straightforward and has some nice characteristics: it avoids unnecessary merging (by using the ‘git rebase’ command where appropriate), it keeps your local master branch stable and detects possible merge conflicts at an early stage. I thought it would be nice to share it in a blog post as a possible inspiration for others. So here it is:

1. Make sure the local master branch is in sync with the upstream. A good strategy for this is to first do a fetch and then a rebase:
git fetch
git rebase origin/master

You can use a ‘git pull’ instead if the local branch and the remote branch have not diverged. Don’t use the pull command if the branches have diverged, because this will result in an additional merge.

2. Create a new branch with a descriptive name for this feature/bugfix:
git checkout -b add-single-sign-on

3. Start developing and testing. Sorry, no code snippet for that one ;-)

4. Commit frequently, but only when all tests pass:
git commit -m "Created db migration for SSO"

5. After every commit, make sure you’re in sync with the upstream:
git fetch
git rebase origin/master

6. When you are ready to push your work to the upstream, repeat the previous step and then switch back to your local master branch:
git switch master

7. Sync the local master branch with the upstream:
git fetch
git rebase origin/master

Again, you can use a ‘git pull’ instead if the local and remote master branches have not diverged.

8. Merge the changes into the local master branch:
git merge add-single-sign-on

9. Push the changes to the upstream:
git push

10. Delete the local branch:
git branch -d add-single-sign-on

In this workflow, you never work directly in the master branch. This way it will always be stable, so you can easily park your current work and branch off your local master branch again when a hotfix is needed.

Your work is only pushed at the end of the line, because prematurely pushing changes can result in unwanted merge conflicts later on. By deleting the local branch at the end, you prevent yourself from continuing to work on changes that have already been pushed and running into this kind of errors.

Posted September 28th, 2009 by Lukas Spee
Tagged with: ,
 

Comments