862

This gives a good explanation of squashing multiple commits:

http://git-scm.com/book/en/Git-Branching-Rebasing

but it does not work for commits that have already been pushed. How do I squash the most recent few commits both in my local and remote repos?

When I do git rebase -i origin/master~4 master, keep the first one as pick, set the other three as squash, and then exit (via c-x c-c in emacs), I get:

$ git rebase -i origin/master~4 master
# Not currently on any branch.
nothing to commit (working directory clean)

Could not apply 2f40e2c... Revert "issue 4427: bpf device permission change option added"
$ git rebase -i origin/master~4 master
Interactive rebase already started

where 2f40 is the pick commit. And now none of the 4 commits appear in git log. I expected my editor to be restarted so that I could enter a commit message. What am I doing wrong?

1

13 Answers 13

1163

Squash commits locally with:

git rebase -i origin/master~4 master

where ~4 means the last 4 commits.

This will open your default editor. Here, replace pick in the second, third, and fourth lines (since you are interested in the last 4 commits) with squash. The first line (which corresponds to the newest commit) should be left with pick. Save this file.

Afterwards, your editor will open again, showing the messages of each commit. Comment the ones you are not interested in (in other words, leave the commit message that will correspond to this squashing uncommented). Save the file and close it.

You will than need to push again with the -f flag.

and then force push with :

git push origin +master

Difference between --force and +

From the documentation of git push:

Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).

26
  • 47
    you can also git push --force origin master
    – Daenyth
    Commented Apr 14, 2011 at 19:05
  • 9
    Daenyth: Yes, but I always prefer this syntax as this is shorter. Commented Apr 14, 2011 at 19:07
  • 104
    And of course, realize that if anyone else might've pulled from the remote repository, you probably don't want to do this - the answer in that case is "you don't."
    – Cascabel
    Commented Apr 14, 2011 at 19:15
  • 10
    Also, I think the OP is exactly copying the command git rebase -i origin/master, and actually wants to know how to rebase commits farther back than that, e.g. git rebase -i origin/master~20 master.
    – Cascabel
    Commented Apr 14, 2011 at 19:16
  • 10
    gstackoverflow: + forces only the refspec which is prefixed by it. --force will force all the refspecs being pushed. Please see the updated answer. Commented Dec 9, 2014 at 23:47
312

On a branch I was able to do it like this (for the last 4 commits)

git checkout my_branch
git reset --soft HEAD~4
git commit
git push --force origin my_branch
12
  • 5
    Doing this with the soft command on an already pushed branch ended up pushing a ton of other peoples commits for me. Commented Feb 4, 2016 at 4:06
  • 6
    A ton? How can it be more than 4? Can you elaborate?
    – jakob-r
    Commented Feb 5, 2016 at 10:08
  • 15
    I would have accepted this as expected answer. More cleaner that accepted answer.
    – vikramvi
    Commented Jul 14, 2016 at 13:17
  • 11
    This is the most clear and accepted answer in just 4 steps Commented Oct 10, 2018 at 13:55
  • 2
    This is a very wrong answer. This will cause a lot of complications if you work with others.
    – tuemar29
    Commented Oct 7, 2022 at 20:20
133

Minor difference to accepted answer, but I was having a lot of difficulty squashing and finally got it.

$ git rebase -i HEAD~4
  • At the interactive screen that opens up, replace pick with squash at the top for all the commits that you want to squash.
  • Save and close the editor

Push to the remote using:

$ git push origin branch-name --force
4
  • 2
    brief and effective, elaborate:
    – terwxqian
    Commented Jan 7, 2019 at 9:35
  • 3
    esc --> :wq provided that the editor is the vi or vim
    – thanos.a
    Commented Jan 22, 2021 at 11:06
  • 3
    Correct and precise. The force push is important to do. Do not pull before pushing. Commented Oct 11, 2021 at 11:37
  • 3
    Thanks for that! Just a note for others, I used +branch-name instead of branch-name --force, as suggested in another answer - should be safer (stackoverflow.com/a/5668050/997940) Commented Jul 5, 2022 at 6:27
39

git rebase -i master

you will get the editor vm open and msgs something like this

Pick 2994283490 commit msg1
f 7994283490 commit msg2
f 4654283490 commit msg3
f 5694283490 commit msg4
#Some message 
#
#some more

Here I have changed pick for all the other commits to "f" (Stands for fixup).

git push -f origin feature/feature-branch-name-xyz

this will fixup all the commits to one commit and will remove all the other commits . I did this and it helped me.

1
  • 1
    This needs to be the top answer and thank you. I find this answer a better way of squashing than blindly doing it based on number of commits . Especially with the VM editor you can do lot of things at once like changing the commit, removing commit and of course squashing. Commented Jul 17, 2020 at 17:22
36

A lot of problems can be avoided by only creating a branch to work on & not working on master:

git checkout -b mybranch

The following works for remote commits already pushed & a mixture of remote pushed commits / local only commits:

# example merging 4 commits

git checkout mybranch
git rebase -i mybranch~4 mybranch

# at the interactive screen
# choose fixup for commit: 2 / 3 / 4

git push -u origin +mybranch
17

1) git rebase -i HEAD~4

To elaborate: It works on the current branch; the HEAD~4 means squashing the latest four commits; interactive mode (-i)

2) At this point, the editor opened, with the list of commits, to change the second and following commits, replacing pick with squash then save it.

output: Successfully rebased and updated refs/heads/branch-name.

3) git push origin refs/heads/branch-name --force

output:

remote:
remote: To create a merge request for branch-name, visit:
remote: http://xxx/sc/server/merge_requests/new?merge_request%5Bsource_branch%5D=sss
remote:To ip:sc/server.git
 + 84b4b60...5045693 branch-name -> branch-name (forced update)
13

Sqush Changes in remote

  • First ensure your local master is at par with remote
  • Then reset your local feature branch to be at par with master: git reset --soft master
  • then add all your modifications and changes (that you made on your local feature branch you just reset to master) to staging area: git add .
  • then commit these changes: git commit -m "this is the final commit message"
  • then force push to remote branch: git push RemoteBranch --force
7

For squashing two commits, one of which was already pushed, on a single branch the following worked:

git rebase -i HEAD~2
    [ pick     older-commit  ]
    [ squash   newest-commit ]
git push --force

By default, this will include the commit message of the newest commit as a comment on the older commit.

7

When you are working with a Gitlab or Github you can run in trouble in this way. You squash your commits with one of the above method. My preferite one is:

git rebase -i HEAD~4
or
git rebase -i origin/master

select squash or fixup for yours commit. At this point you would check with git status. And the message could be:

    On branch ABC-1916-remote
    Your branch and 'origin/ABC-1916' have diverged,
    and have 1 and 7 different commits each, respectively.
      (use "git pull" to merge the remote branch into yours)

And you can be tempted to pull it. DO NOT DO THAT or you will be in the same situation as before.

Instead push to your origin with:

git push origin +ABC-1916-remote:ABC-1916

The + allow to force push only to one branch.

3
  • 1
    there's nothing wrong on pulling, specially if you have conflicts they can be resolved easier than if you force push
    – Ray_Poly
    Commented Mar 3, 2020 at 11:00
  • @Ray_Poly - won't that just put those remote commits back as local commits? That's what I've found.
    – Steve Dunn
    Commented Dec 11, 2020 at 12:13
  • yes, it'll create a new commit, but you can see in that commit the differences, better than re-writing the git history everytime you rebase
    – Ray_Poly
    Commented Dec 11, 2020 at 14:39
2

For whom might be interested, this is how it looks like when you squash commits in GitHub PR.

Before: enter image description here

After:

enter image description here

1

In my case requirement was to squash all the feature branch commits into one, to have a clean commit history. Utilized the GitHub UI to do so.

Problem:

  • Feature branch (eg:featureBranch) has been created from master (a month ago).
  • Committed all my changes to the featureBranch (~12 commits, a month of work). And has been pushed to remote regularly.

Requirement:

  • To Get the feature branch updated with the master and to have a single commit in a featureBranch

Steps followed:

  • Create a new branch (eg: featureBranchLatest) from master in GitHub UI.
  • Create a PR from featureBranch to featureBranchLatest.
  • Resolve conflicts if any. Merge the PR with the squash commit option in GitHub UI. (Alter the commit message to have a cleaner message).

Now the featureBranchLatest will have a single commit of all the changes needed in a single commit, along with the latest changes from the master. Delete the old branch featureBranch if not required for reference.

1

Many of the proposed solutions require doing a git push --force at the end. In my case, the git repository was configured to disallow git push --force, so I was stuck.

Here's how I went about squashing my commits on the feature-branch:

  1. Switch to main: git switch main
  2. Create a new branch from main: git switch -c feature-branch-2
  3. Cherry-pick every commit from feature-branch that you want to squash (without committing): git cherry-pick -n <hash>
  4. Commit all the changes (cherry-pick -n has automatically staged them): git commit -m "New message"
  5. Push the new branch: git push origin -u feature-branch-2
  6. Delete your old local branch: git branch -D feature-branch

If you have the necessary permissions, you can also delete the old remote branch. Otherwise, you can ask someone with the appropriate permissions to do it, or simply wait for it to become stale.

0

Merge (rebase) commits in a specifc branch

List all specifc branch commits

MODEL

git cherry -v <ORIGINAL_BRANCH> <YOUR_BRANCH>

EXAMPLE

git cherry -v main some_branch_name

[Ref(s).: https://stackoverflow.com/a/24668421/3223785 ]

If there is more than one commit in the created branch, perform a rebase to centralize everything in just one commit.

TIP: This process is important for organizing and ensuring that all changes made in one branch can be brought over to another branch in a cherry pick process if this is the scenario.

Merge (rebase) commits

The rebase is basically a "merge" of commits.

MODEL

git rebase -i origin/<YOUR_BRANCH>~<NUMBER_COMMITS_TO_REBASE> <YOUR_BRANCH>

EXAMPLE

git rebase -i origin/some_branch_name~2 some_branch_name

NOTE: The <NUMBER_COMMITS_TO_REBASE> means the number of commits you are going to merge (rebase) - that is, the number of commits displayed by the "List all specifc branch commits" command - and -i means interactive mode.

A text editor will open. Keep only the commits (hash) shown by the "List all branch commit..." command. Keep the oldest commit (it will appear first) as "pick" and the others in sequence change to "squash". This configuration file after being saved will be executed as a batch by git.

MODEL

git push origin +<YOUR_BRANCH>

EXAMPLE

git push origin +some_branch_name

IMPORTANT: To force a push TO ONLY ONE BRANCH (<YOUR_BRANCH>), use a + in front of it to push.

[Ref(s).: https://stackoverflow.com/a/5668050/3223785 ]

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