Explore Courses Blog Tutorials Interview Questions
+2 votes
in DevOps and Agile by (19.4k points)

I usually submit a list of commits for review. If I have the following commits:

  1. HEAD
  2. Commit3
  3. Commit2
  4. Commit1

...I know that I can modify head commit with git commit --amend. But how can I modify Commit1, given that it is not the HEAD commit?

1 Answer

+1 vote
by (27.5k points)
edited by

Show the last 4 commits in a text editor and find the commit you want to modify,

git rebase -i @~4  

Remember that @ is shorthand for HEAD, and ~ is the commit before the specified commit.

Now, replace pick to e (edit) and save and close the file. Git rewinds that commit, allowing you to either:

  • use the command, git commit --amend to make changes, or
  • use the command, git reset @~ to discard the last commit, but not the changes to the files

The second one is useful for doing more complex stuff like splitting into multiple commits.

After that run the command, git rebase --continue, and then Git will replay the subsequent changes on top of your modified commit. You may be asked to fix some merge conflicts.

For more information please go through the following tutorial to get more info about git:


Browse Categories