101

I made a pull request on GitHub. Now the owner of the repository is saying to squash all the commits into one.

When I type git rebase -i Notepad opens with the following content:

noop

# Rebase 0b13622..0b13622 onto 0b13622
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I searched on Google but I do not understand how to do this.

3
  • 1
    Duplicate of Squash my last X commits together using Git.
    – user456814
    Commented Jun 6, 2014 at 8:13
  • 5
    You don't have to squash anymore: the owner can do it for you (since March 2016): see stackoverflow.com/a/36377439/6309
    – VonC
    Commented Apr 2, 2016 at 18:52
  • Another option is to create a new branch based on whichever branch you want the pull request to be merged into (i.e. usually main or master), squash-merge into the new branch, and create the pull request from that branch. IMHO that's a lot easier and less confusing than rebasing. Squash-merging can be easily done in many GIT-applications, such as Github Desktop. The only downside is that now you have two branches, but if you don't need to keep your original commits, you can safely delete the original one after squash-merging into the new one.
    – Magnus
    Commented Apr 10 at 8:14

4 Answers 4

97

Just a simple addition to help someone else looking for this solution. You can pass in the number of previous commits you would like to squash. for example,

git rebase -i HEAD~3 

This will bring up the last 3 commits in the editor.

4
  • 14
    Doing git rebase -i HEAD~2 for me brings up an editor with like 20 lines of commits. And none of them are the last two commits I want. What's going on?
    – B T
    Commented Jun 26, 2015 at 17:57
  • 5
    Probably your previous commit is a merge. Commented Oct 29, 2015 at 13:57
  • See also @omerjerk's answer below about editing the last 2 commits from pick to squash
    – frankster
    Commented Jul 25, 2016 at 10:34
  • 7
    this isn't useful if you've merged in a branch like master that has more recent updates Commented Aug 29, 2020 at 14:24
52

ok I figured it out ... First I had to write git rebase -i xxxxxxxxxxxxxxxx where xxxxxxxxxx is the SHA of the commit upto which I've to squash. Then in Notepad I edited the first as pick and rest of all as squash. Then a new notepad window will come and there in the first line I typed the name of my new commit. And then I had to do a force push :

git push --force origin master
7
  • 10
    It's not a good idea to force push or rebase a branch that is already pushed to remote. All other people who had the un-rebased master branch will get an error, and they will be confused. Commented Oct 16, 2014 at 10:45
  • 5
    what does "up to" mean exactly? Is xxxxxxxxxxx included or not included?
    – sebnukem
    Commented Mar 19, 2015 at 17:10
  • 4
    @luckykrrish How does one squash commits on a public pull request then? Commented Dec 3, 2015 at 12:58
  • 3
    @luckykrrish That's a perfectly acceptable thing to do on your own PR branch.
    – Léo Lam
    Commented Dec 26, 2015 at 20:45
  • 1
    I got conflict. Thank you very much for this. No one mentions about this in no other answer. Commented Sep 17, 2020 at 16:56
42

As of April 1, 2016, the repository's manager can squash all the commits in a pull request into a single commit by selecting "Squash and merge" on a pull request.

Squash and merge option

If you want to manually squash commits in a pull request, refer to fontno's answer.

11

Try git rebase -i, and use 'squash' for all the commits you want to squash.

Edit:

git rebase -i will show you an interactive editor with the list of commits you are rebasing. The default command before each commit is "pick", so you just need to s/pick/squash/ for all the commits you want to squash, and then all of them will be squash into their last previous commit.

Make sure you are rebasing on a correct branch.

10
  • 7
    I didn't get it.. I already said I know nothing of these things.
    – omerjerk
    Commented Jan 26, 2013 at 6:16
  • git rebase -i will show you an interactive editor with the list of commits you are rebasing. The default command before each commit is "pick", so you just need to s/pick/squash/ for all the commits you want to squash.
    – Cong Wang
    Commented Jan 26, 2013 at 6:20
  • git rebase -i is followed by a branch name, make sure you have read the last sentence in my answer.
    – Cong Wang
    Commented Jan 26, 2013 at 7:06
  • 2
    when you write "s/pick/squash/", what is the "s" option?
    – AlanSE
    Commented Aug 27, 2015 at 13:57
  • 3
    It's sed syntax (Stream EDitor), a very powerful but arcane Linux tool. The s command stands for "switch" (or replace), followed by a search string and a replacement string, separated by slashes. Usually followed by "g", which means "global", or "do this replacement not just once but everywhere". So s/pick/squash/g means: "replace every instance of pick with squash". Commented Sep 15, 2016 at 8:40

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