Skip to content

Instantly share code, notes, and snippets.

@vHeemstra
Forked from aortbals/squash-and-merge-cli.md
Created July 24, 2022 17:56
Show Gist options
  • Select an option

  • Save vHeemstra/4f18bc86f79e89e45b94324b5e0db550 to your computer and use it in GitHub Desktop.

Select an option

Save vHeemstra/4f18bc86f79e89e45b94324b5e0db550 to your computer and use it in GitHub Desktop.

Revisions

  1. @aortbals aortbals revised this gist Mar 30, 2022. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions squash-and-merge-cli.md
    Original file line number Diff line number Diff line change
    @@ -6,51 +6,51 @@ Let's talk through two ways to do a squash and merge on the command line.

    **When to use it**

    - When you have not merged master into your feature branch
    - When you have not merged main into your feature branch
    - There are no merge conflicts
    - When you want to retain the original committer on the squashed commit

    **Steps**

    You are working on branch `feat-fuu`. You want to create a single squashed commit to push to your remote `feat-fuu` branch on GitHub.

    1. `git checkout master`
    1. `git checkout main`
    1. `git pull`
    1. `git checkout feat-fuu`
    1. `git checkout feat-fuu-backup`
    - *Optional but recommended* - make a backup version of your branch
    1. `git checkout feat-fuu`
    1. `EDITOR='code -w' git rebase -i master`
    1. `EDITOR='code -w' git rebase -i main`
    - Setting `EDITOR` is optional, and depends on your editor of choice. With the case of VSCode or Sublime Text, the `-w` flag tells the editor to "wait" until the file exits before closing.
    1. In your editor, edit all of the additional commits to `squash`. Leave the first commit in the list alone
    1. Save and exit your editor
    1. Rewrite a nice single commit message for the commit
    1. Check the history. `feat-fuu` will now contain a single commmit ahead of `master`
    1. Check the history. `feat-fuu` will now contain a single commmit ahead of `main`
    1. `git push -f origin feat-fuu`
    - **Please be careful with this step, as it overwrites your original remote branch on GitHub**

    ## Take 2: Git Squashed Merge

    **When to use it**

    - You have merged master into your branch and resolved conflicts
    - You have merged main into your branch and resolved conflicts
    - You don't care about the author of the original commits (you will be rewriting it)

    You are working on branch `feat-fuu`. You want to create a single squashed commit to push to your remote `feat-fuu` branch on GitHub.

    1. `git checkout master`
    1. `git checkout main`
    1. `git pull`
    1. `git checkout feat-fuu`
    1. `git checkout feat-fuu-backup`
    1. `git checkout master`
    1. `git checkout main`
    1. `git branch -D feat-fuu`
    - **You are deleting your original branch.** Ensure you have created `feat-fuu-backup` beforehand and it has your full commit history.
    1. `git checkout -b feat-fuu`
    - This creates a fresh branch from master
    - This creates a fresh branch from main
    1. `git merge --squash feat-fuu-backup`
    - You are merging and squashing your original work into a single commit. This is where the magic happens.
    1. Rewrite a nice single commit message for the commit
    1. Check the history. You should see a single commmit on your branch that branched from `master`
    1. Check the history. You should see a single commmit on your branch that branched from `main`
    1. `git push -f -u origin feat-fuu`
    - **Please be careful with this step, as it overwrites your original branch on GitHub**

  2. @aortbals aortbals revised this gist Apr 15, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions squash-and-merge-cli.md
    Original file line number Diff line number Diff line change
    @@ -25,8 +25,8 @@ You are working on branch `feat-fuu`. You want to create a single squashed commi
    1. In your editor, edit all of the additional commits to `squash`. Leave the first commit in the list alone
    1. Save and exit your editor
    1. Rewrite a nice single commit message for the commit
    1. Check the history. You should see a single commmit on your branch that branched from `master`
    1. `git push -f`
    1. Check the history. `feat-fuu` will now contain a single commmit ahead of `master`
    1. `git push -f origin feat-fuu`
    - **Please be careful with this step, as it overwrites your original remote branch on GitHub**

    ## Take 2: Git Squashed Merge
  3. @aortbals aortbals revised this gist Nov 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion squash-and-merge-cli.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ You are working on branch `feat-fuu`. You want to create a single squashed commi
    1. `git checkout master`
    1. `git branch -D feat-fuu`
    - **You are deleting your original branch.** Ensure you have created `feat-fuu-backup` beforehand and it has your full commit history.
    1. `git checkout feat-fuu`
    1. `git checkout -b feat-fuu`
    - This creates a fresh branch from master
    1. `git merge --squash feat-fuu-backup`
    - You are merging and squashing your original work into a single commit. This is where the magic happens.
  4. @aortbals aortbals revised this gist Nov 30, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions squash-and-merge-cli.md
    Original file line number Diff line number Diff line change
    @@ -23,11 +23,11 @@ You are working on branch `feat-fuu`. You want to create a single squashed commi
    1. `EDITOR='code -w' git rebase -i master`
    - Setting `EDITOR` is optional, and depends on your editor of choice. With the case of VSCode or Sublime Text, the `-w` flag tells the editor to "wait" until the file exits before closing.
    1. In your editor, edit all of the additional commits to `squash`. Leave the first commit in the list alone
    1. Save and exit my editor
    1. Save and exit your editor
    1. Rewrite a nice single commit message for the commit
    1. Check the history. You should see a single commmit on your branch that branched from `master`
    1. `git push -f`
    - **Please be careful with this step, as it overwrites your original branch**
    - **Please be careful with this step, as it overwrites your original remote branch on GitHub**

    ## Take 2: Git Squashed Merge

  5. @aortbals aortbals revised this gist Nov 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion squash-and-merge-cli.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ You are working on branch `feat-fuu`. You want to create a single squashed commi
    - *Optional but recommended* - make a backup version of your branch
    1. `git checkout feat-fuu`
    1. `EDITOR='code -w' git rebase -i master`
    - Setting `EDITOR` is optional, and depends on your editor of choice. With the case of VSCode or Sublime Text, the `-w` flag tells the editor to "wait" until the file exists before closing.
    - Setting `EDITOR` is optional, and depends on your editor of choice. With the case of VSCode or Sublime Text, the `-w` flag tells the editor to "wait" until the file exits before closing.
    1. In your editor, edit all of the additional commits to `squash`. Leave the first commit in the list alone
    1. Save and exit my editor
    1. Rewrite a nice single commit message for the commit
  6. @aortbals aortbals renamed this gist Nov 30, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @aortbals aortbals created this gist Nov 30, 2017.
    59 changes: 59 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    With the introduction of GitHub's [Squash and Merge](https://github.com/blog/2141-squash-your-commits) feature, this has become less prevelant, however it's still useful in scenarios where GitHub's interface is unavailable.

    Let's talk through two ways to do a squash and merge on the command line.

    ## Take 1: Interactive Rebase

    **When to use it**

    - When you have not merged master into your feature branch
    - There are no merge conflicts
    - When you want to retain the original committer on the squashed commit

    **Steps**

    You are working on branch `feat-fuu`. You want to create a single squashed commit to push to your remote `feat-fuu` branch on GitHub.

    1. `git checkout master`
    1. `git pull`
    1. `git checkout feat-fuu`
    1. `git checkout feat-fuu-backup`
    - *Optional but recommended* - make a backup version of your branch
    1. `git checkout feat-fuu`
    1. `EDITOR='code -w' git rebase -i master`
    - Setting `EDITOR` is optional, and depends on your editor of choice. With the case of VSCode or Sublime Text, the `-w` flag tells the editor to "wait" until the file exists before closing.
    1. In your editor, edit all of the additional commits to `squash`. Leave the first commit in the list alone
    1. Save and exit my editor
    1. Rewrite a nice single commit message for the commit
    1. Check the history. You should see a single commmit on your branch that branched from `master`
    1. `git push -f`
    - **Please be careful with this step, as it overwrites your original branch**

    ## Take 2: Git Squashed Merge

    **When to use it**

    - You have merged master into your branch and resolved conflicts
    - You don't care about the author of the original commits (you will be rewriting it)

    You are working on branch `feat-fuu`. You want to create a single squashed commit to push to your remote `feat-fuu` branch on GitHub.

    1. `git checkout master`
    1. `git pull`
    1. `git checkout feat-fuu`
    1. `git checkout feat-fuu-backup`
    1. `git checkout master`
    1. `git branch -D feat-fuu`
    - **You are deleting your original branch.** Ensure you have created `feat-fuu-backup` beforehand and it has your full commit history.
    1. `git checkout feat-fuu`
    - This creates a fresh branch from master
    1. `git merge --squash feat-fuu-backup`
    - You are merging and squashing your original work into a single commit. This is where the magic happens.
    1. Rewrite a nice single commit message for the commit
    1. Check the history. You should see a single commmit on your branch that branched from `master`
    1. `git push -f -u origin feat-fuu`
    - **Please be careful with this step, as it overwrites your original branch on GitHub**

    ## References

    - This [Stack Overflow Post](https://stackoverflow.com/a/2427520) has additional detail about the differences between these two approaches.