Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to amend last 2nd commit or specific commit only

Tags:

git

In my current branch, I've 2 commits which are not pushed yet.

mainlineBranch:
 commit 2 - recent commit
 commit 1

I've few code changes & want to amend only to commit 1 without deleting commit 2. Can someone please help me ?

like image 421
aishu8333 Avatar asked Dec 02 '25 06:12

aishu8333


1 Answers

git rebase --interactive helps you.

First, stash your uncommitted changes for later use (git stash). Your working directory should be clean now. Run git rebase -i HEAD~2 and your editor will pop up with the following content:

pick  "commit 1"
pick  "commit 2"

Change pick into edit for commit 1 and exit the editor. You can then git stash pop the changes onto the working tree, which should be at commit 1 now. Amend commit 1 with git commit --amend and continue the rebase with git rebase --continue. Assume conflicts are handled, you should now land on a new HEAD with a modified commit 1 and an identical commit 2.

like image 125
iBug Avatar answered Dec 03 '25 19:12

iBug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!