Monday, May 31, 2021

Git Rebase

Today I did a squash commit with git rebase. These were the steps taken to achieve this:

  1. Rebase interactively against master

    git rebase -i master
    
  2. Replace the p with an s next to each commit message

  3. Continue the rebase after resolving conflicts

    git rebase --continue
    
  4. Force push

    git push origin master --force
    

I still don't fully understand how it works and I should do some more practice with just some example projects or read the rebase chapter again for the git book. This also seems like a good post on squash commits specifically.

Review

I practiced this over the weekend with some basic squash commits in a main branch and it was pretty easy! The basic notation you need to remember is:

git rebase -i <commit-id>

You then use the p or s notation to pick (keep original commit) or squash (combine commits). You also need a tad of vim knowledge if you're using that as the editor but it's not too tricky. I'm not exactly what the use case of passing a branch is (like the above) but if your intention us just to squash some commits together in the same branch then commit-id is the way to go.

This will also only work if you're working with more than 3 commits in a branch. If you want to merge just 2 commits together in a branch the commands are:

git reset --soft "HEAD^"
git commit --amend