Technical Diary of a Dummy

Thoughts and Opinions that will live on after they have vacated my brain.

Rebasing Git Commits -

James Land - 25 Jan 2021

Squashing Git Commits

A quick and dirty on combining multiple commits. Can be used to clean up commit history before merging a personal branch.

Steps

Run Rebase Command

Running it interactively makes it easier:

git rebase -i HEAD~<# Of commits>

Modify rebase file:

Rebase Example

pick b2149dc Commit we want to keep
s c5489da Commit to lose

# Rebase 263e7ff..b2149dc onto 263e7ff (1 command)

Update commit message

Commit Message File

# This is a combination of 2 commits.
# This is the 1st commit message:

Commit we want to keep

# This is the commit message #2:

#Just comment out the line below to remove it from your commit message
Commit we want to lose

Push to git

Requires a forced push since you are rewriting history

git push -f

IMPORTANT

Rebasing commits in git will DESTROY ALL THE COMMITS YOU REBASE. Make sure you are careful when you check in.

You should not rebase if:

The main intent of rebasing is to squash multiple commits on a local branch into one before pushing back to your main branch in order to keep the git history clearer. And make you seem smarter, you know for all those people who go back and look at git commit history… I pity those people


Tips and Tricks

Replace picks with squash s inside your rebase file:

:%s/pick/s

Something went wrong? Abort the rebase with:

git rebase --abort
Tags:
github git