Git with team: Mastering efficiency

Patrik Krupar
UOL Devs
Published in
2 min readMay 10, 2017

Most of us (over 42.9% in 2014) use git to track our code changes. We are old friends with commit and push, it’s second nature to us because we use them all the time in our personal projects. However, teamwork usually requires more consistency across commit messages, sticking to a good branching strategy and a bunch of new mysterious flags and commands. In this, I’d like to touch on commonly used commands in teams and how to not waste time with them.

by Wesley Caribe

The workflow: theory

At UOL our workflow when working on a new feature or bug is following:

  1. create branch per ticket (tracked through Redmine); with name scheme developerName/ticketNumer_ticketName, which in practise looks as follows kruparp/346_redesign_dashboard
  2. rebase all commits to a single one
  3. merge our feature branch to main development branch
  4. push to origin

The workflow: the real deal

Create a new a branch

git checkout -b kruparp/346_redesign_dashboard

and after the feature is done (in this case dashboard is redesigned)

git checkout developmentgit pull origin developmentgit checkout kruparp/346_redesign_dashboard# How many commits is this branch ahead of development?
git rev-list --count HEAD ^development
# Turns out I made 4 commits that I should squash to a single one
git rebase -i HEAD~4
git rebase developmentgit checkout developmentgit merge kruparp/346_redesign_dashboardgit push origin development

That’s a lot of commands, let’s speed things up.

Speeding up the proccess

Now it’s time for some git alias magic.

git count

Returns number of commits feature branch is ahead of development.

count = "!f() { compareBranch=${1-development}; git rev-list --count HEAD ^$compareBranch; }; f"

git squashbase

Starts a interactive rebase so we can squash all commits to a single one.

squashbase = "!f() { branchName=${1-development}; commitCount=$(git count $branchName); git rebase -i HEAD~$commitCount; }; f"

git pullbase

Updates development branch and start rebasing feature branch into development branch.

pullbase = "!f() { branchName=${1-development}; git checkout $branchName && git pull && git checkout - && git rebase $branchName; }; f"

and now rebasing to development is simple as

# If more than 1 commit use git squashbase
# Also assuming we're in feature branch (kruparp/346_redesign_dashboard)
git squashbase
git pullbase
git checkout development
git merge kruparp/346_redesign_dashboard
git push origin development

Credit for these git aliasis goes to @BuddyLReno, https://medium.freecodecamp.com/bash-shortcuts-to-enhance-your-git-workflow-5107d64ea0ff.

That’s a wrap. We simplified merging feature branches into main development branch (from 9 to 5 commands) due to cleverly set up git aliases.

--

--