How to Squash 3 commits into 1?
Asked Answered
P

4

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.

Prose answered 29/5, 2017 at 19:24 Comment(0)
R
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'
Riebling answered 29/5, 2017 at 19:51 Comment(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.Prose
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.Riebling
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.)Callas
L
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.

Lanham answered 29/5, 2017 at 19:42 Comment(0)
D
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

Dore answered 29/5, 2017 at 19:36 Comment(3)
I don't think I have write permissions in the repoProse
Well thats fine just tell whoever is in charge to do it.Dore
But the merger has.Wooldridge
F
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
Foreshorten answered 29/5, 2017 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.