Collaborating with Git 201

git logo
Git (source)

In this post, I’m going to review the git commands that will make your workflow with git more advanced and efficient.

This post has the following structure:

  1. git cherry-pick
  2. git stash
  3. git merge vs git rebase

The basics of collaboration with Git and GitHub could be found in this post:

git-rebase — Reapply commits on top of another base tip

Git cherry-pick 🍒

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.

Cherry picking is the act of picking a commit from a branch and applying it to another.

When to use git cherry-pick

  1. For undoing changes and restoring lost commits. For example, say a commit is accidentally made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.
  2. For team collaboration. For example, you know the exact change in the exact commit on your partner’s branch you need to grab to continue working on your feature.
  3. For bug hotfixes. For an example scenario, say a developer has started work on a new feature. During that new feature development, they identify a pre-existing bug. The developer creates an explicit commit patching this bug. This new patch commit can be cherry-picked directly to the master branch to fix the bug before it affects more users.

Git cherry-pick usage

$ git checkout feature-branch
$ git cherry-pick xx007

Cherry-picking is a powerful and convenient command that is incredibly useful in a few scenarios. Cherry-picking should not be misused in place of git merge or git rebase. The git log command is required to help find commits to cherry-pick.

Git Stash 📦

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.

git stash options

git stash options by Atlassian

Git stash usage

1. Pulling from upstream

$ git pull # fail
$ git stash
$ git pull # success
$ git stash pop # success

2. Switching branches

$ git stash
$ git checkout very-old-release # fail
# do something
$ git stash pop

Git merge vs. Git rebase 🔁🔀🔂

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

An example of git merge: merging the main branch into feature branch, and possible visualisation:

$ git checkout feature
$ git merge main

The git rebase command allows you to reapply commits on top of another base tip. Rebasing is the process of moving or combining a sequence of commits to a new base commit.

The golden rule of thegit rebase command is to never use it on public branches.

An example of git rebase: rebasing the feature branch onto main branch, and possible visualisation:

$ git checkout main
$ git rebase feature

The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

Pros and cons of git merge and git rebase

git merge

✅ Easy to find related commits
✅ Easy to revert
✅ Only solve conflicts once
❗️ Merge commit difficult to review
❗️ The individual commits not always representative

git rebase

✅ Keeps history linear and tidy
✅ Not trivial to revert
❗️ Sometimes multiple conflict resolutions required
❗️ No hidden changes
✅ Conflicts resolved exactly where they occur

For more details:

Software Engineer | React | JavaScript | Ruby on Rails