Home > GIT > GIT merge or squash 2 or more commits into 1

GIT merge or squash 2 or more commits into 1

git lets you squash 2 or more commits into 1 commit before pushing to upstream


you have a git repo
you make some changes and do a commit with message: commit 1
you make some more changes and do another commit with message: commit 2
you can then merge these 2 commits into 1 single commit before pushing your changes upstream
here is the concept on how to do it

My Work Environment

  • Windows 7 – 64 bit
  • git version 1.9.5.msysgit.0
  • git bash installed
  • notepad++ editor installed ( i will be using this as my default editor )

open gitbash

change your default editor to notepad++
this is for Windows 7 64 bit – make sure you have the correct path of the editor for your machine

git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

for testing – create a folder in your file system
open gitk and cd into the folder
initialize the test git repo

git init

create a file 1.txt
do the first commit on master

git add .
git commit -m "commit 1"

create a branch from master and checkout development

git branch development
git checkout development

create a file 2.txt and commit to development

git add .
git commit -m "commit 2"

create a file 3.txt and commit to development

git add .
git commit -m "commit 3"

at this point gitk should show you 3 commits on development branch
we will squash commit 2 and commit 3 into 1 commit with a final commit message

the HEAD~2 is for last 2 commits, for last 3 commits HEAD~3

git rebase -i HEAD~2

this will open up notepad++
you will see 2 commits at the top
pick ebbea73 commit 2
pick fd2055f commit 3

the first word is important: pick
you will keep the first commit the same ( pick ) and change all commits after that to an s ( for squash )
so the file will look like ( the commit hash will be different – this is just an example )

pick ebbea73 commit 2
s fd2055f commit 3

lets say you had 4 commits, then your file would look like ( s = squash at the beginning )

pick ebbea73 commit 2
s fd2055f commit 3
s eccea73 commit 4
s ftt055f commit 5

save and close the file
git should say rebasing and open another window for the commit message
this will be used as the commit message for the squashed commit
you can comment out ( with a hash # ) the first 2 commit messages and add your new commit message at the bottom
the file will look like this ( note that commit 2 and commit 3 messages have been commented out with a # )

# This is a combination of 2 commits.
# The first commit's message is:
#commit 2
# This is the 2nd commit message:
#commit 3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 7500cac
# You are currently editing a commit while rebasing branch 'development' on '7500cac'.
# Changes to be committed:
# new file: 2.txt
# new file: 3.txt

save and close
git should say successfully rebased and updated refs/heads/development
gitk should show the new commit message and only 2 commits

Categories: GIT Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: