Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created November 29, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantuck/c306af0eed2e2a7504aacc30b723a005 to your computer and use it in GitHub Desktop.
Save ryantuck/c306af0eed2e2a7504aacc30b723a005 to your computer and use it in GitHub Desktop.
How to squash commits, for when you forget

How to squash commits, for when you forget:

• ~/src $$$ mkdir git-stuff
• ~/src $$$ cd git-stuff/
• ~/src/git-stuff $$$ git init
Initialized empty Git repository in /Users/ryan.tuck/src/git-stuff/.git/
• ~/src/git-stuff $$$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
• ~/src/git-stuff $$$ touch a
• ~/src/git-stuff $$$ git add .
• ~/src/git-stuff $$$ git commit -m 'add a'
[master (root-commit) 53f649d] add a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
• ~/src/git-stuff $$$ git log
commit 53f649dff6ffadf662f89b43e45eca4e94c3e8b1 (HEAD -> master)
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:08 2018 -0600

    add a
• ~/src/git-stuff $$$ touch b
• ~/src/git-stuff $$$ git add .
• ~/src/git-stuff $$$ git commit -m 'add b'
[master d7c9960] add b
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b
• ~/src/git-stuff $$$ gs
On branch master
nothing to commit, working tree clean
• ~/src/git-stuff $$$ git log
commit d7c99609743aab184ae213a34bd907beea1a82f2 (HEAD -> master)
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:29 2018 -0600

    add b

commit 53f649dff6ffadf662f89b43e45eca4e94c3e8b1
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:08 2018 -0600

    add a
• ~/src/git-stuff $$$ touch c
• ~/src/git-stuff $$$ git add .
• ~/src/git-stuff $$$ git commit -m 'add c'
[master 8fa6d86] add c
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c
• ~/src/git-stuff $$$ ls
a b c
• ~/src/git-stuff $$$ git log
commit 8fa6d8670e9b309b9ba2a29eab5a889d00386bd7 (HEAD -> master)
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:40 2018 -0600

    add c

commit d7c99609743aab184ae213a34bd907beea1a82f2
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:29 2018 -0600

    add b

commit 53f649dff6ffadf662f89b43e45eca4e94c3e8b1
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:08 2018 -0600

    add a

By this point, we've added 3 commits to this repo. Now, I wanna squash the last two commits for b and c into a single commit.

Type git rebase -i head~2, which will bring up an interactive thing:

pick <hash-1> add b
pick <hash-2> add c

Just pick the first one and squash the rest

pick <hash-1> add b
squash <hash-2> add c

Next, you'll be taken to a window where you can completely update the commit message that will encapsulate this whole change. Add a commit message and :wq.

• ~/src/git-stuff $$$ git rebase -i head~2
This script requires vim7.0+ with Python 3.6 support.
Press ENTER or type command to continue
hint: Waiting for your editor to close the file... This script requires vim7.0+ with Python 3.6 support.
Press ENTER or type command to continue
[detached HEAD e8005a0] actually add b and c as one commit
 Date: Thu Nov 29 09:35:29 2018 -0600
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b
 create mode 100644 c
Successfully rebased and updated refs/heads/master.
• ~/src/git-stuff $$$ git log
commit e8005a00dbf74007f61f345c50558779d5d2ef18 (HEAD -> master)
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:29 2018 -0600

    actually add b and c as one commit

commit 53f649dff6ffadf662f89b43e45eca4e94c3e8b1
Author: Ryan Tuck <ryntck@gmail.com>
Date:   Thu Nov 29 09:35:08 2018 -0600

    add a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment