git rebase

Title Text

Title Text

Title Text

GIT MERGE EXAMPLE

MERGE

  • adds an extra commit every time you merge origin/master into your branch
  • attempts to merge everything in one shot
  • keeps the commits in chronological order

REBASE

  • takes all commits on your current branch and moves them all after the latest origin/master commit
  • attempts to merge smaller chunks of code one at a time 
  • changes the history of your commits (timestamp and commit hash changes)

rebase example

  • we have a branch named ARC-0000

  • and you've done some work on it (3 commits)

git log --oneline

a4c0d8f ARC-0000 - finished refactoring the thing
d54ae91 ARC-0000 - wip - started refactoring a super important thing
f0b1ed4 ARC-0000 - made some changes
5bb3d18 Merge pull request #535 from wapopartners/ARC-1546

* the base commit is 5bb3d18

rebase example

  • meanwhile commits have been made to origin/master

  • branches have diverged

  • need to sync with origin/master again

git log --oneline

1d2b99f Merge pull request #540 from wapopartners/ARC-1502
62679d9 Merge pull request #536 from wapopartners/ARC-1501
5bb3d18 Merge pull request #535 from wapopartners/ARC-1546

* the base commit NOW is 1d2b99f

rebase example

  • use rebase to reply all commits on ARC-0000 after the latest commit from origin/master

git rebase origin/master

# things happen

git log --oneline

97324cf ARC-0000 - finished refactoring the thing
bdedb9e ARC-0000 - wip - started refactoring a super important thing
47a7e81 ARC-0000 - made some changes
1d2b99f Merge pull request #540 from wapopartners/ARC-1502

* commits are replayed after 1d2b99f, also the commit hash values have changed

rebase example

rebase mode

  • git rebase --continue (you would run this after you have fixed a conflict staged and want to continue the rebasing process)

  • git rebase --abort (if all else fails, you can always abort)
  • git rebase --skip (will skip your commit)

before rebase - SOURCETREE

after rebase - SOURCETREE

590dd54 ARC-1545: minor code improvements. Sets textklass only in condition where it's needed...
9f1e2b9 ARC-1545: adds bottom margin
be57ccd ARC-1545: added comment and removed unnecessary code
102475e ARC-1545: adjusts artice-subhead enriched article class to be more like Escenic version...
a1b4dc8 Merge branch 'ARC-1520' into ARC-1545
92f9a5c ARC-1545: adjusts subheading generated code to keep original enriched article class
a08d42a Merge branch 'master' into ARC-1520
a0eacad ARC-1520: adds rule to override inline enriched blockquote styling to pull ARC version of font
d1e3910 1.1.42
25a6b3a Merge pull request #494 from wapopartners/ARC-1504
27fb845 ARC-1504: ADDED test for lib/identity/api.js
78a3aad ARC-1504: ADDED check the service string as the primary source of a user's subscriber status
7d5be0f 1.1.41
c164cd9 ARC-1452: (master) Unit test for video/manager.js and powa.js (#481)
1d2b99f 1.1.40
62679d9 Merge pull request #525 from wapopartners/revert-522-ARC-1525
7b7293a Revert "ARC-1525 : AdOps page target breakpoint (master)"
4032408 Merge pull request #522 from wapopartners/ARC-1525
8e32e23 1.1.39
5a5cba3 Merge pull request #498 from wapopartners/ARC-1514
f665252 ARC-1525 Corrects issue
70992e0 ARC-1520: adds noFigureStyles styling to make bleeds work again after change in HTML structure.
17f9699 fixing camel cased variables
a3a8193 ARC-1520: adjusts colour for enriched article-subhead, as requested
            
            
            
            
            
            

before rebase - terminal

125e336 ARC-1545: minor code improvements. Sets textklass only in condition where it's needed...
dc98085 ARC-1545: adds bottom margin
6c0287a ARC-1545: added comment and removed unnecessary code
64d628d ARC-1545: adjusts artice-subhead enriched article class to be more like Escenic version for...
4b4eab5 ARC-1545: adjusts subheading generated code to keep original enriched article class
7e9b1bb ARC-1520: adds rule to override inline enriched blockquote styling to pull ARC version of font
b4318ab ARC-1520: adds noFigureStyles styling to make bleeds work again after change in HTML structure.
645781d ARC-1520: adjusts colour for enriched article-subhead, as requested
d1e3910 1.1.42
            

AFTER rebase - TERMINAL

INTERACTIVE REBASE

rewrite your history

INTERACTIVE REBASE

  • extremely helpful especially if your branch is polluted with a lot of merge and other unwanted commits.
  • say you made some quick eslint changes (for example), and you want it to be squashed into one commit
  • in this case, you want to do an interactive rebase on the last 2 commits.

INTERACTIVE REBASE

  • in this case, you want to do an interactive rebase on the LAST 2 commits.
  • will replay the last two commits locally and will bring up your default editor for git commit messages
git rebase -i HEAD~2

INTeractive TODO LIST

pick c164cd9 eslint fixes [Chester Rivas]
pick a4c0d8f finished refactoring the thing [Chester Rivas]

# Rebase c164cd9..a4c0d8f onto f0b1ed4 (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.
#

f IT

f c164cd9 eslint fixes [Chester Rivas]
p a4c0d8f finished refactoring the thing [Chester Rivas]

# Rebase c164cd9..a4c0d8f onto f0b1ed4 (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.
#

interactive rebase complete

  • save and quit
  • force push - since you have rewritten the history of your branch you have to do a force push

*ideally you want to squash your commits into milestones in your code or any logical order that makes sense to you.

don't rebase master

don't rebase master

  1. the master branch (and other shared branches) used for PRs should continue being merged
  2. shared branches have different authors and the history should be preserved because anyone can branch from a commit at any time
  3. if you rebase on master the commits will change and if someone else branches off from a commit that no longer exists 

other common mistakes

  1. make sure you have exited out of rebase mode before continuing to work
  2. don't rebase of your local master, always origin/master, that way you're guaranteed to be fetching the latest and origin/master
  3. when it doubt, accept "their" changes from server, you can always re-add your changes later
  4. be careful with dropping (d) commits, you might get rid of something you need
  5. if you have messed up your branch after you've finished rebasing and you want to rebase again, reset your branch to origin.
git reset --hard origin/ARC-0000

quick recap

  • rebase your branches only

  • rebase with origin/master first, then origin/development if necessary

  • squash commits to keep the history clear and concise

  • git rebase --abort to exit out of rebase mode

  • use tools like SourceTree to view the commit timeline

  • use WebStorm's or Atom's merge conflict tools to resolve conflicts

git checkout ARC-000
git rebase origin/master

git push

#######################

# interactive and squashing commits

# last 5 commits locally

git rebase -i HEAD~5

# all commits from the time you branched off to your latest local commit

git rebase -i origin/master

# force push

git push -f

WORKFLOW EXAMPLE

.GITCONFIG

[rebase]
  instructionFormat = %s [%an]

*this will format each line when you run git interactive

Git Rebase 2

By Chester Rivas

Git Rebase 2

outlining the benefits of git rebase

  • 952