0

I have commits like this, 1 is the newest, and three is the oldest:

  • commit 1
  • commit 2
  • commit 3

How to remove commit 1 and 2, but preserves the changes and commit it to the commit 3?

adnntfr
  • 102
  • 4

2 Answers2

1

To reset last two commit you can use git reset HEAD~2, it will remove the commit. After the commit is removed you can amend the oldest commit so it will include the changes of commit 1 and commit 2, git add --all then git commit --amend.

alcatraz
  • 264
  • 1
  • 14
  • If you reset two commits back then amend rather than just committing, you'll go one commit too far in history and impact `commit 3`'s parent. – Romain Valeri Jul 07 '21 at 08:17
  • I don't know what do you mean by `one commit too far` and `impact commit 3 parent`, can you elaborate it? I think that will happen if we `git reset HEAD~3` rather than 2. If we just make a new commit isn't it will be `commit 3, new commit` rather than `commit 3` having all the changes by itself? – alcatraz Jul 07 '21 at 08:27
  • First `reset` goes back 2 commits, so at this point `HEAD` points to `commit 3`. Then `git commit --amend` implies re-doing the previous commit, so `commit 3`'s *parent*. – Romain Valeri Jul 07 '21 at 08:35
  • @RomainValeri `git commit --amend` redoes the *current* commit. I'm not sure how it would work to redo the previous one (as then it would affect the current one also, so every `--amend` would alter 2 commits in that case) – seumasmac Jul 07 '21 at 10:20
  • i checked it here https://www.oreilly.com/library/view/git-pocket-guide/9781449327507/ch04.html, it won't affect the parent at all, i think you @RomainValeri maybe read somewhere and have a wrong understanding about `previous commit`, `previous commit` in this case means the `last commit`, not means the `previous` of the `last` commit. – alcatraz Jul 07 '21 at 13:11
  • @alcatraz No, but I guess you might have misunderstood my comment. When you do `--amend`, you're not committing on top of current commit, you're **redoing** and replacing it. Read whatever you want, but this is a wrong answer. – Romain Valeri Jul 07 '21 at 13:20
  • @RomainValeri yeah, it's redoing and replacing it, so it will be a new commit, but it's not affect the parent at all, and the new commit parent will be the same as the replaced commit parent – alcatraz Jul 07 '21 at 13:20
  • @RomainValeri does it mean there's no way to keep the `commit 3` with all the changes? So it either replace `commit 3` with the new one like this answer, or build a `new commit` after the `commit 3` like the answer below? – adnntfr Jul 07 '21 at 14:07
1

You can run git reset --soft HEAD~2 to move the HEAD branch back to an older commit (the most recent commit you want to keep).

And then simply run git commit again with your desire changes. Before that add all the files which you want to keep your update commit.

If you want details about this please go through the bellow link. Git-Reset Demystified

RafsanJany
  • 355
  • 1
  • 10