-
Notifications
You must be signed in to change notification settings - Fork 0
Backend Training: Git Squash
🔖 : Git Squash
, VIM
It is always a good practice to commit your code regularly to avoid losing history; however, when merging branches, we need to make sure that our git histories are nice and clean and make sense for other people. This is when we will need to rewrite our Git history.
Let's begin by viewing our git histories
$ git log
commit 8a4fcefbf81fc8109280733658541ff80b32a4f2
Author: Nelson Lee <[email protected]>
Date: Sat Nov 4 16:31:56 2017 -0700
Add I18n to layout components
commit 50c535b729cde60ee9d21cc94de52118582c4c80
Author: Nelson Lee <[email protected]>
Date: Sat Nov 4 15:40:47 2017 -0700
Setup layout components
commit e8e2db04c8c840df4e73dd25a74daa717442f508
Author: Nelson Lee <[email protected]>
Date: Fri Nov 3 11:32:31 2017 -0700
Initial Setup
The last 2 commits are both about layout components so let's make them into one single commit.
Let's rollback 2 commits by
$ git rebase -i HEAD~2
pick 50c535b Setup layout components
pick 8a4fcef Add I18n to layout components
# Rebase e8e2db0..8a4fcef onto e8e2db0 (2 commands)
#
# 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
# d, drop = remove commit
#
# 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.
-- INSERT --
Press a on your keyboard to start editing.
Change the second line pick 8a4fcef Add I18n to layout components
to..
s 8a4fcef Add I18n to layout components
Press esc on your keyboard to exit editing mode.
Type :x
and hit enter to save and exit. This should open up ...
# This is a combination of 2 commits.
# This is the 1st commit message:
Setup layout components
# This is the commit message #2:
Add I18n to layout components
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Nov 4 15:40:47 2017 -0700
#
# interactive rebase in progress; onto e8e2db0
# Last commands done (2 commands done):
# pick 50c535b Setup layout components
# s 8a4fcef Add I18n to layout components
# No commands remaining.
Remove the commit messages accordingly by press dd
to leave only the first one. You can move the cursor using the arrow keys ← → ↑ ↓
The result should be
# This is a combination of 2 commits.
Setup layout components
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Nov 4 15:40:47 2017 -0700
#
# interactive rebase in progress; onto e8e2db0
# Last commands done (2 commands done):
# pick 50c535b Setup layout components
# s 8a4fcef Add I18n to layout components
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on 'e8e2db0'.
#
# Changes to be committed:
# modified: Gemfile.lock
# new file: app/controllers/home_controller.rb
# new file: app/views/components/_footer.slim
Type :x
and hit enter to save and exit. This should open up ...
[detached HEAD 07a2896] Setup layout components
Date: Sat Nov 4 15:40:47 2017 -0700
11 files changed, 112 insertions(+), 75 deletions(-)
create mode 100644 app/controllers/home_controller.rb
create mode 100644 app/views/components/_footer.slim
create mode 100644 app/views/components/_navbar.slim
create mode 100644 app/views/home/about.slim
create mode 100644 app/views/home/index.slim
create mode 100644 app/views/home/pricing.slim
rewrite app/views/layouts/application.slim (93%)
rewrite config/locales/en.yml (99%)
create mode 100644 spec/controllers/home_controller_spec.rb
Successfully rebased and updated refs/heads/master.
Check your Git histories again
$ git log
commit 07a2896a8261958ff8d27ce1ee9c024ec8799dc7
Author: Nelson Lee <[email protected]>
Date: Sat Nov 4 15:40:47 2017 -0700
Setup layout components
commit e8e2db04c8c840df4e73dd25a74daa717442f508
Author: Nelson Lee <[email protected]>
Date: Fri Nov 3 11:32:31 2017 -0700
Initial Setup
As you can see, the last 2 commits have been combined into a single commit.