Skip to content

Instantly share code, notes, and snippets.

@patik
Last active July 24, 2025 08:48

Revisions

  1. patik revised this gist Nov 3, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions how-to-squash-commits-in-git.md
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@

    This method avoids merge conflicts if you have periodically pulled master into your branch. It also gives you the opportunity to squash into more than 1 commit, or to re-arrange your code into completely different commits (e.g. if you ended up working on three different features but the commits were not consecutive).

    *Note: You cannot use this method if you intend to open a pull request to merge your feature branch.* This method requires committing directly to master.
    ***Note: You cannot use this method if you intend to open a pull request to merge your feature branch.* This method requires committing directly to master.**

    Switch to the master branch and make sure you are up to date:

    ```sh
    git checkout master && git fetch && git pull
    git checkout master && git pull
    ```

    Merge your feature branch into the master branch locally:
  2. patik revised this gist Jan 26, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion how-to-squash-commits-in-git.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@

    This method avoids merge conflicts if you have periodically pulled master into your branch. It also gives you the opportunity to squash into more than 1 commit, or to re-arrange your code into completely different commits (e.g. if you ended up working on three different features but the commits were not consecutive).

    *Note: You cannot use this method if you intend to open a pull request to merge your feature branch.* This method requires committing directly to master.

    Switch to the master branch and make sure you are up to date:

    ```sh
    @@ -35,7 +37,7 @@ git commit

    This method only allows you to squash the last X consecutive commits into a single commit. Also, if you have merged master into your branch along the way, you will have to manually merge your new (squashed) commit into master and resolve the merge conflicts.

    Use this method if you have not merged master into your branch, you're plan to combine all commits into one, and you only changed one feature of the project.
    Use this method if you have not merged master into your branch, you plan to combine all commits into one, and you only changed one feature of the project; or, regardless of those conditions, you must use this method if you intend to open a pull request to merge your code.

    ### Combining the commits

  3. patik revised this gist Jan 25, 2016. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions how-to-squash-commits-in-git.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    ## Squashing commits the easy way
    ## Squashing Git Commits

    ## The easy and flexible way

    This method avoids merge conflicts if you have periodically pulled master into your branch. It also gives you the opportunity to squash into more than 1 commit, or to re-arrange your code into completely different commits (e.g. if you ended up working on three different features but the commits were not consecutive).

    @@ -29,7 +31,11 @@ git commit

    ([Source](http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit))

    ## Squashing commits the hard way
    ## The hard(er) and less flexible way

    This method only allows you to squash the last X consecutive commits into a single commit. Also, if you have merged master into your branch along the way, you will have to manually merge your new (squashed) commit into master and resolve the merge conflicts.

    Use this method if you have not merged master into your branch, you're plan to combine all commits into one, and you only changed one feature of the project.

    ### Combining the commits

  4. patik revised this gist Jan 25, 2016. 1 changed file with 34 additions and 1 deletion.
    35 changes: 34 additions & 1 deletion how-to-squash-commits-in-git.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,37 @@
    ### Squashing the commits
    ## Squashing commits the easy way

    This method avoids merge conflicts if you have periodically pulled master into your branch. It also gives you the opportunity to squash into more than 1 commit, or to re-arrange your code into completely different commits (e.g. if you ended up working on three different features but the commits were not consecutive).

    Switch to the master branch and make sure you are up to date:

    ```sh
    git checkout master && git fetch && git pull
    ```

    Merge your feature branch into the master branch locally:

    ```sh
    git merge feature_branch
    ```

    Reset the local master branch to origin's state:

    ```sh
    git reset origin/master
    ```

    Now all of your changes are considered as unstaged changed. You can stage and commit them into one or more commits.

    ```sh
    git add . --all
    git commit
    ```

    ([Source](http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit))

    ## Squashing commits the hard way

    ### Combining the commits

    To squash the last **3** commits into one:

  5. patik created this gist Jul 21, 2015.
    31 changes: 31 additions & 0 deletions how-to-squash-commits-in-git.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    ### Squashing the commits

    To squash the last **3** commits into one:

    ```sh
    git reset --soft HEAD~3
    git commit -m "New message for the combined commit"
    ```

    ### Pushing the squashed commit

    #### If the commits **have** been pushed to the remote:

    ```sh
    git push origin +name-of-branch
    ```

    *The plus sign forces the remote branch to accept your rewritten history, otherwise you will end up with divergent branches*

    #### If the commits have **NOT** yet been pushed to the remote:

    ```sh
    git push origin name-of-branch
    ```

    In other words, just a normal push like any other

    ---

    Main source: http://stackoverflow.com/a/5201642/348995
    Source for info about when commits where already pushed: http://stackoverflow.com/a/5668050/348995