How Do I Modify a Specific Commit?

Better Stack Team
Updated on June 24, 2024

To modify a specific commit in Git, you can use an interactive rebase. Here's how you can do it:

Step 1: Start an Interactive Rebase

Open your terminal and run:

 
git rebase -i HEAD~<number-of-commits>

Replace <number-of-commits> with the number of commits you want to include in the interactive rebase. For example, to include the last 3 commits, you would use HEAD~3.

Step 2: Mark the Commit to Be Edited

In the interactive rebase editor that opens, locate the commit you want to modify. Change the word "pick" to "edit" (or simply "e") for that commit.

Step 3: Amend the Commit

Once you've marked the commit for editing, save and close the editor. Git will then pause the rebase process at the specified commit.

Make the desired changes to your files. After you've made your changes, stage them using:

 
git add .

Then, amend the commit using:

 
git commit --amend

Step 4: Continue the Rebase

After amending the commit, continue the rebase using:

 
git rebase --continue

Note:

  • If you're modifying the most recent commit, you can also use git commit --amend without starting an interactive rebase.
  • Be cautious when modifying commits, especially if they have already been pushed to a remote repository and shared with others. Modifying commit history can cause issues for collaborators.
  • Interactive rebases rewrite commit history, so you should avoid modifying commits that have already been pushed to a shared repository.
  • After modifying a commit, its hash will change, which may require force-pushing if it has been pushed to a remote repository.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github