Skip to content

Instantly share code, notes, and snippets.

@louissalin
Last active August 29, 2015 14:04
Show Gist options
  • Save louissalin/bec446f440fe59662c7d to your computer and use it in GitHub Desktop.
Save louissalin/bec446f440fe59662c7d to your computer and use it in GitHub Desktop.
small git merging exercise
$ mkdir git-exercises
$ cd git-exercises
$ git init
$ touch a.txt
$ git add -A
$ git commit -m 'empty file'

##let’s work on a small feature

$ git checkout -b small_feature
  • create a file name b.txt and add the word “hello” in there
$ git add -A
$ git commit -m 'new file with text'
  • edit b.txt and change the text to “hello, world!”
$ git add -A
$ git commit -m ‘forgot to add full text’

we are done with the small feature! The individual commits don’t really matter here so we don’t necessarily want to keep them around when we merge down to master. So in this case, we’ll do a squash merge

$ git checkout master
$ git merge --squash small_feature

a “git status” would now show b.txt as a new file, staged, but not committed yet, so let’s commit it

$ git commit -m 'small feature'

we can now delete the branch since it’s fully merged in. Note that we have to force delete the branch because git doesn’t see it as fully merge. That’s because commits in the branch and master differ due to the squash. That’s okay, though, we know it’s merged in. You can try doing a “git diff master small_feature” if you want. You shouldn’t see any differences between the two branches.

$ git branch -D small_feature

##let’s work on a large feature

$ git checkout -b large_feature
  • create a file name c.txt and add the word “step 1” in there
$ git add -A
$ git commit -m 'new file with step 1 done’
  • edit c.txt and add a new line: “fix issue with step 1”
$ git add -A
$ git commit -m ‘fix bug
  • edit c.txt and add a new line: “step 2”
$ git add -A
$ git commit -m 'step 2’
  • edit c.txt and add a new line: “step 3”
$ git add -A
$ git commit -m 'step 3’
  • edit c.txt and add a new line: “fix all the bugs”
$ git add -A
$ git commit -m 'fix a bunch of bugs'

we’re done with our large feature. We want to merge it into master, but we don’t want to lose the important commit. So we won’t do a squash merge here. However, the two bug fix commits are not very important and we’d like to get rid of them.

$ git rebase -i master

you should see something like this:

pick 5649098 new file with step 1 done
pick 427a342 fix bug
pick 1a1dc57 step 2
pick 6350901 step 3
pick 99933e1 fix a bunch of bugs

# Rebase e9a368b..99933e1 onto e9a368b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

what you can do is described in the comments. What we want to do is meld the 2nd commit into the 1st, and meld the last commit into the 4th.

  • edit the file like so:
pick 5649098 new file with step 1 done
squash 427a342 fix bug
pick 1a1dc57 step 2
pick 6350901 step 3
squash 99933e1 fix a bunch of bugs
  • save and exit the editor

a new editor will come up for each squash you did. Each will have the two squashed commits’ messages and you now have the opportunity to create a new commit message for the combination of the two previous commits.

  • change the first commit message to “step 2”
  • save and exit
  • change the 2nd commit message to “step 3 with bug fixes”
  • save and exit

you should now be back in your shell

$ git log

you should now see only 3 commits, disregarding the commits done before work on large_feature

commit af595186b82faf5808bcac5f358e1588cd67475e
Author: Louis Salin <louis.phil@gmail.com>
Date:   Tue Jul 29 16:17:20 2014 -0500

    step 3 and bug fixes

commit c3205dabf5217dc23bf7a8a8b6c6d32149deb3a2
Author: Louis Salin <louis.phil@gmail.com>
Date:   Tue Jul 29 16:16:49 2014 -0500

    step 2

commit 8e73605a6befb47ba7320383e32a415809019102
Author: Louis Salin <louis.phil@gmail.com>
Date:   Tue Jul 29 16:14:55 2014 -0500

    new file with step 1 done

note that this process also replayed your work on top of the master branch. You effectively squashed commits, then replayed them over the latest changes of the master branch

$ git checkout master
$ git merge large_feature

now let’s delete the branch. Note that we don’t have to force anything this time since the commits in both branches are the same

$ git branch -d large_feature
$ git log

you should not see and MERGE commits in the log

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