1

Say there is a sequence: Commit 1 -> Commit 2 -> Commit 3. I'd like to forget that Commit 1 and Commit 2 ever existed, such that Commit 3 assumed to be a single existing commit, however all the changes made by both Commit 1 and Commit 2 are "backed-in" the Commit 3.

How can I achieve that?

P.S. It is going to be master branch, if this matters.

Zazaeil
  • 3,900
  • 2
  • 14
  • 31
  • Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – UnholySheep Dec 06 '18 at 16:38
  • Possible duplicate of [How to squash all git commits into one?](https://stackoverflow.com/questions/1657017/how-to-squash-all-git-commits-into-one) – phd Dec 06 '18 at 16:57
  • The fact that one question have the same answer than other doesn't mean they are duplicates. Your "possible duplicates" references question about how to do something that by the way is the solution to this problem. However there are more solutions, such as using checkout --orphan, that could not be the solution to those questions. – Adrian Dec 06 '18 at 19:25
  • 1
    If those were exact duplicates then the eftshift0's answer should apply to them as well, but it is not always the case. – Adrian Dec 06 '18 at 19:26

2 Answers2

2

You start over with a new branch with the working tree as you have it in front of you with git checkout --orphan

eftshift0
  • 26,375
  • 3
  • 36
  • 60
1

i'm not sure if my answer is ideal or not, but the way i did is using

git rebase -i --root

after that, squash the second commit to the first commit. also don't forget to pick the 3rd commit. by edit the interactive rebase prompt in your console,

pick 3609c82 commit 1
squash 21d643a commit 2
pick 0ce4e9c commit 3

# Rebase 3da6375 onto db86c06 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

save then exit,..

if there any conflict happen you can resolve and do "git rebase --continue". or you can also abort the rebase using "git rebase --abort"

you also can rename the the commit by using amend.

remember, always do backup before you changed any history like rebase squash or amend. because from what i understand, it is not revert/reset able. that's why i said, maybe this is not ideal, but that is what i did and it worked.

have a nice day!

hifebriansyah
  • 341
  • 1
  • 6