Adding changes to a previous git commit

Greg Foster
Greg Foster
Graphite software engineer

This guide will cover several methods to add changes to a previous commit, including using git commit --amend, interactive rebase, and amending a merge commit. We'll also discuss how to handle scenarios when the changes have already been pushed to a remote repository.

If you need to make changes to the most recent commit, to add missing files or update the commit message for example, you can use git commit --amend. This is the simplest form of revising a commit.

Let's walk through how to use the git commit --amend command in a few common scenarios.

  1. Stage the additional files you want to include in the last commit:

    Terminal
    git add <file-path>

    Replace <file-path> with the path to the file you want to add.

  2. Amend the last commit

    If you want to add the file without changing the commit message, run:

    Terminal
    git commit --amend --no-edit

    This command adds the staged changes to the last commit while keeping the same commit message.

  3. Amend the commit message

    If you want to add the files and also edit the commit message you can do so by running:

    Terminal
    git commit --amend -m "New commit message"

    This command will still add teh staged changes to the last commit, but will also replace the commit message with the new message specified by the -m flag.

If the commit you want to modify is not the most recent one, you can use git rebase in interactive mode to edit any commit in your project’s history.

  1. Start an interactive rebase for the last n commits:

    Terminal
    git rebase -i HEAD~n

    Replace n with the number of commits back from the current HEAD that includes the commit you want to edit.

  2. Select the commit you wish to edit Let's assume you have decided to change a commit message and you start the rebase with:

Terminal
git rebase -i HEAD~3

This command initiates an interactive rebase of the last three commits. After running this, Git opens your configured text editor with a file that lists the recent commits:

Terminal
pick e3a1b35 Add navigation bar
pick 97ac9d8 Update background color
pick f30ab87 Fix typo in README

The list of commits you see is in reverse chronological order, meaning the newest commit is at the top. Each line represents a commit, and the word pick tells Git to take the commit as is.

To amend the commit where you updated the background color, you change the word pick to edit next to the corresponding commit:

Terminal
pick e3a1b35 Add navigation bar
edit 97ac9d8 Update background color
pick f30ab87 Fix typo in README

After you've made this change, save and close the editor. Git will then reapply the commits up to the one you've marked for editing and pause, allowing you to amend the commit. The terminal will reflect this by returning control to you and showing a message like:

Terminal
Stopped at 97ac9d8... Update background color
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue

At this point, you can change the commit message or make other amendments as necessary.

  1. Make your changes in the working directory

    Create new files or modify existing ones as needed.

  2. Stage the changes for commit by running:

    Terminal
    git add <file-path>
  3. Amend the commit:

    Terminal
    git commit --amend
  4. Continue the rebase:

    Terminal
    git rebase --continue

    Repeat this process if you have marked more than one commit for editing.

If you have already pushed the commit you amended or rebased, you will need to force push the changes.

Once you have completed the steps to amend the commit, to force push your changes, re-writing the history of the commit that was already pushed, run:

Terminal
git push --force

This can overwrite history on the remote, so it should be done with caution, especially in a collaborative environment.

For further reading on adding changes to a previous Git commit, see the official documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2