1

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 ?

aishu8333
  • 41
  • 1
  • 10

1 Answers1

1

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~3 and your editor will pop up with the following content:

pick  aaaaaaa
pick  bbbbbbb
pick  ccccccc

In the above example, bbbbbbb should be your "commit 1" and ccccccc should be your "commit 2". Change pick into edit for bbbbbbb 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 --confinue. Assume conflicts are handled, you should now land on a new HEAD with a modified commit 1' and an identical commit 2'.

iBug
  • 35,554
  • 7
  • 89
  • 134