Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

git - How to amend last 2nd commit or specific commit only

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 ?

question from:https://stackoverflow.com/questions/65851445/how-to-amend-last-2nd-commit-or-specific-commit-only

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...