96

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.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
omerjerk
  • 4,090
  • 5
  • 40
  • 57
  • 1
    Duplicate of [Squash my last X commits together using Git](http://stackoverflow.com/q/5189560/456814). –  Jun 06 '14 at 08:13
  • 5
    You don't have to squash anymore: the owner can do it for you (since March 2016): see http://stackoverflow.com/a/36377439/6309 – VonC Apr 02 '16 at 18:52

4 Answers4

88

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.

fontno
  • 6,642
  • 6
  • 36
  • 43
51

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
omerjerk
  • 4,090
  • 5
  • 40
  • 57
37

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.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
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.

Cong Wang
  • 2,001
  • 12
  • 13
  • 6
    I didn't get it.. I already said I know nothing of these things. – omerjerk Jan 26 '13 at 06: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 Jan 26 '13 at 06:20
  • `git rebase -i` is followed by a branch name, make sure you have read the last sentence in my answer. – Cong Wang Jan 26 '13 at 07:06
  • ok I got it. and squashed all the changes into a new commit . but when clicked on sync then those commits again came. should i do something else after squashing ? – omerjerk Jan 26 '13 at 07:26
  • 2
    when you write "s/pick/squash/", what is the "s" option? – AlanSE Aug 27 '15 at 13:57
  • "s/pick/squash" means "replace pick with squash". – Léo Lam Dec 26 '15 at 20:46
  • 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`". – Amedee Van Gasse Sep 15 '16 at 08:40
  • This is a very helpful answer! Thanks! – Fisher Coder Jan 15 '17 at 20:50
  • It's a great discussion but it does not answer the question what one should actually do. `error: invalid line 3: s/pick/squash/g` does not seem to be a successful execution – Anna Leonenko Aug 05 '22 at 20:05
  • This is pathologically wrong with git – Niklas Rosencrantz Jun 14 '23 at 04:54