sandeepk

git

Git rebase is a very handy command to integrate changes from one branch to another, we can run git rebase in two modes manual and interactive. In manual all commit take from the current branch and applied over the head of the passed branch, but in case of the interactive rebase command you have more control over the option to what do with commits.

So to understand how rebase work, we take an example where we have a master branch with the following commits.

>>> git log
commit 8bfb8c19c3d7b795e9698a9818880d89ca3c214a
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:06:07 2020 +0530

    New goals added

from this master branch, we create a new branch dev and do some changes/bug fixes.

>>> git checkout -b dev master
...
>>> git log
commit a05b6cd75e604df0f4434a574809a4fc14e4313e
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:08:11 2020 +0530

    workaround bugs

commit 8bfb8c19c3d7b795e9698a9818880d89ca3c214a
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:06:07 2020 +0530

    New goals added

but in between the other developer push changes in the master branch and to integrate that changes in your current branch you can use merge or rebase command, the rebase helps you maintain the liner history of your workflow.

>>> git checkout master
>>> git log
commit 3c5d6baf13aeac37d9efb1218bbf3240ec5c2a12
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:07:29 2020 +0530

    new release added

commit 8bfb8c19c3d7b795e9698a9818880d89ca3c214a
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:06:07 2020 +0530

    New goals added

so now to integrate new changes from master to your branch dev, without making the commit history complex, we can use rebase command, let's check out

>>> git checkout dev
>>> git rebase master
>>> git log
commit 184805896dd5684fc076b9bb9aa34eb3994251b1
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:08:11 2020 +0530

    workaround bugs

commit 3c5d6baf13aeac37d9efb1218bbf3240ec5c2a12
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:07:29 2020 +0530

    new release added

commit 8bfb8c19c3d7b795e9698a9818880d89ca3c214a
Author: Sandeep <sandeepchoudhary1507@gmail.com>
Date:   Sun Jun 14 01:06:07 2020 +0530

    New goals added

we can also run the rebase command in —interactive mode which gives us the option to edit/squash/... the commits

>>> git rebase -i master
pick 1848058 work around bugs

# Rebase 3c5d6ba..1848058 onto 3c5d6ba (1 command(s))
#
# 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.
#
# Note that empty commits are commented out

One of the cool use of the git rebase command is that you can change the base of the branch from one branch to other by use of —onto option.

Let assume you create a branch featureA from master and then another branch featureB from featureA, to change the base of the featureB branch.

# git rebase --onto <newbase> <oldbase>
>>> git rebase --onto master featureA featureB

So this is all about the git rebase command, which can help you to keep your commit history clean and your current working branch commits sync with the master branch.

#git