Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Change a Git Commit in the Past with Amend and Rebase Interactive

change-a-git-commit-in-the-past-with-amend-and-rebase-interactive.jpg

In this video we'll go over a few examples ranging from changing the last commit message to rebasing a commit sometime in the past.

Quick Jump:

Being able to amend your commits is a very useful skill to master because after developing a feature for a while you might decide that a specific commit in the past needs to be altered to add a recent change or perhaps you just want to fix a typo in the commit message.

It’s especially handy in local development to help you create useful commit messages before you push your changes since this does involve rewriting history.

By the end of this video you’ll be able to change your commits in a matter of seconds using the plain old git client on the command line without needing any special tools or editor plugins.

# Demo Video

Commands

Amend the most recent commit:

# [Add your changes with git add -p, etc.]

# Change the last commit with a new commit message.
git commit --amend

# Change the last commit with the existing commit message.
git commit --amend ---no-edit

Amend a commit in the past:

# Figure out which commit you want to edit by getting its SHA.
git log

# Start an interactive rebase ($SHA = your commit's SHA and the ^ is important!).
git rebase --interactive $SHA^

# [Change 'pick' to 'edit' for your commit and save the buffer]

# [Add your changes with git add -p, etc.]

# Change the commit and optionally add --no-edit if you want to keep the existing message.
git commit --amend

# Finalize and apply the rebase.
git rebase --continue

# Or cancel the rebase and go back to what it was like before you started rebasing.
git rebase --abort

If you already have files you’ve changed before you start rebasing you can run git stash before starting the rebase and then git stash pop after finishing the rebase. This could be handy if you have uncommitted work that you don’t want to apply to the rebase.

Amend just the commit message in the past:

# Figure out which commit you want to edit by getting its SHA.
git log

# Start an interactive rebase ($SHA = your commit's SHA and the ^ is important!).
git rebase --interactive $SHA^

# [Change 'pick' to 'reword' for your commit and save the buffer]

# [Modify the commit message however you see fit and save the buffer]

The above is a quick way to edit an older commit message.

Timestamps

  • 0:20 – Use cases for wanting to change a commit
  • 1:03 – The project we’ll be amending commits in
  • 2:04 – Amending the last commit message
  • 3:15 – Amending a file in the last commit without editing the message
  • 5:44 – Adding or removing multiple files and amending the last commit
  • 6:20 – Preparing to do an interactive rebase
  • 7:25 – Figuring out which commit to amend using HEAD~N or the direct SHA
  • 9:15 – Starting an interactive rebase with git rebase –interactive [SHA]^
  • 10:18 – Making our changes as part of the interactive rebase
  • 11:39 – Adding our changes and amending the commit during the rebase
  • 12:33 – Pulling the trigger and finalizing the rebase
  • 13:21 – Aborting a rebase in case you decide not to apply it
  • 14:44 – Using git stash to rebase a commit without mixing in our un-staged changes
  • 17:35 – Recap and do you have any questions?

Reference Links

What commands or tools do you use to amend commits? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments