-1

I forked a repository, made a branch from master, made two commits to it, and sent a pull request. Now, I want to squash the commits in that branch to one so that it shows only one commit in the pull request.

Can someone explain with steps, how it can be done?

  • Do you want to retain the log messages and show that the new commit is a combination of several, or just create a new commit with a log message that does not mention the original commits? – William Pursell Mar 21 '21 at 12:31
  • 2
    Does this answer your question? [Git: How to squash all commits on branch](https://stackoverflow.com/questions/25356810/git-how-to-squash-all-commits-on-branch) – Joe Mar 21 '21 at 13:03
  • Squashing the commits was easy when you are on the master branch. But I am not able to understand how to squash when you are on a sub-branch. I just want all the commits after the first commit to be squashed into the first one. – Rohan Gupta Mar 21 '21 at 14:16
  • Like, pick <1st commit> squash <2nd commit> squash <3rd commit> – Rohan Gupta Mar 21 '21 at 14:17

2 Answers2

0

You can do git rebase -i HEAD~[amount of commits]. Usually a vim editor opens.

To edit you press 'i'. After that you can use the first commit and change the word pick to squash on the other commits.

After you are done editing press escape and after that type wq which will write the changes and then quit. As a last step you do a git push -f

Daniel Jacob
  • 1,455
  • 9
  • 17
0

If you want to completely discard the original history and not retain any references to the abandoned commits in the log, you can simply do:

git reset $branch_point 
git commit -a -m "new message"

(This assumes you are starting with a clean working directory checked out at the commit you want the branch to be on.) The initial reset does not modify the working directory, but just sets the branch back to the master branch at merge base (eg, branch_point=$( git merge-base HEAD master )), and then makes a new commit.

If you want to retain references to all of the old commits, just do a git merge --squash

William Pursell
  • 204,365
  • 48
  • 270
  • 300