13

I'm fairly new to git, so I've been trying to figure out how to squash 3 commits into 1 so my PR can get merged. I've read a lot of docs and guides and sort of found out how to squash commits but one of my commits aren't showing up. I tried attaching a photo of my commits and a photo of my terminal when I type in: "git rebase -i HEAD~5", but unfortunately I'm a new user so I can't? But anyway, I would greatly appreciate some help.

4 Answers 4

20

How to Squash 3 commits into 1?

  • do git log and confirm the commits you want to squash are commited, first.
  • if you want squash top 3 commits from your head,

Use this command,

git rebase -i HEAD~3

Here the tilde ~ symbol with 3 pick the top latest three commits and it will pop up in an interactive shell where you can select the top commit and squash other two commits into one by entering s, it means squash.

If your commit is not appearing then do git log and see, if not then

git add files
git commit -m 'your commit'
3
  • I've already tried git rebase HEAD~3 and when I do git log they show up, but not when I do rebase. I believe this would be much simpler if I could show you my commits and terminal outputs.
    – Kaycee
    Commented May 29, 2017 at 20:02
  • type git reflog in your terminal and it shows the history what you did. after git rebase you have to push that to the origin. if you want to push it to master, do git push master or do branch, do git push origin your_branch. Commented May 29, 2017 at 20:07
  • When you rebase -i you have to edit the second and third commits to be squashed instead of picked; this will require knowing how to edit files in the editor. (Alternatively, Git GUIs should generally have some way for you to rebase and squash.)
    – Pockets
    Commented May 29, 2017 at 20:20
1

Squashing of commits means creation of a new commit instead of several. git reset to the base commit of your branch and make a new one. Then just git push -f to your branch.

0

Hey so GitHub has Squash And Merge option when merging a PR. So nothing you need to do on your end, just use option Squash and Merge when merging.

more info here https://help.github.com/articles/about-pull-request-merges/#squash-and-merge-your-pull-request-commits

3
  • I don't think I have write permissions in the repo
    – Kaycee
    Commented May 29, 2017 at 19:54
  • Well thats fine just tell whoever is in charge to do it.
    – Ivan86
    Commented May 29, 2017 at 19:58
  • But the merger has.
    – wumpz
    Commented May 29, 2017 at 20:12
0

As of April 1, 2016 it is now possible for the repository administrator to perform Squashing of pull requests. However if you have been requested to do this yourself then:

  1. This SO response covers it
  2. This article by Eli Bendersky covers this as well

Not the answer you're looking for? Browse other questions tagged or ask your own question.