Thursday, 27 December 2018

GIT: Editing history with the interactive rebase

Git allows you to edit your commit history with a functionality called interactive rebase. For example, you can combine several commits into one commit, reorder or skip commits and edit the commit message.
This is useful as it allows the user to rewrite some commit history (cleaning it up) before pushing the changes to a remote repository.
Interactive rebase allows you to quickly edit a series of commits using the following actions:
Table 3. Interactive rebase actions
ActionDescription
pick
includes the selected commit, moving pick entries enables reordering of commits
skip
removes a commit
edit
amends the commit
squash
combines the changes of the commit with the previous commit and combines their commit messages
fixup
squashes the changes of a commit into the previous commit discarding the squashed commit’s message
reword
similar to pick but allows modifying the commit message
The setup for the rebase is called the rebase plan. Based on this plan, the actual interactive rebase can be executed.
It is safe to use interactive rebase as long as the commits have not been pushed 
to another repository. As the interactive rebase creates new commit objects, 
other developers might be confused if you rebase already published changes.

37.1. Example: Interactive rebase

The following commands create several commits which will be used for the interactive rebase.
# create a new file
touch rebase.txt

# add it to git
git add . && git commit -m "add rebase.txt  to staging area"

# do some silly changes and commit
echo "content" >> rebase.txt
git add . && git commit -m "add content"
echo " more content" >> rebase.txt
git add . && git commit -m "just testing"
echo " more content" >> rebase.txt
git add . && git commit -m "woops"
echo " more content" >> rebase.txt
git add . && git commit -m "yes"
echo " more content" >> rebase.txt
git add . && git commit -m "add more content"
echo " more content" >> rebase.txt
git add . && git commit -m "creation of important configuration file"

# check the git log message
git log
We want to combine the last seven commits. You can do this interactively via the following command.
git rebase -i HEAD~7
This command opens your editor of choice and lets you configure the rebase operation by defining which commits to picksquash or fixup.
The following listing shows an example of the selection. We pick the last commit, squash 5 commits and fix the sixth commit. The listing uses the long format of the commands (for example fixup instead of the short form f ) for better readability.
pick 7c6472e rebase.txt added to index
fixup 4f73e68 added content
fixup bc9ec3f just testing
fixup 701cbb5 ups
fixup 910f38b yes
fixup 31d447d added more content
squash e08d5c3 creation of important configuration file

# Rebase 06e7464..e08d5c3 onto 06e7464
#
# 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.

0 comments:

Post a Comment