Interactive rebase in VScode with GitLens

September 14, 2022

Overview:

  • rebasing is the process of moving or combining a sequence of commits to a new base commit

Use case:

  • feature branch may become out of date with main branch, requiring a merge from main to incorporate new changes
  • rebasing allows you to keep your branch history clean and appear as though you've been working off the main branch

Requirements:

  • install the GitLens extension
  • configure VSCode to be the default git editor
[core]
  editor = code --wait

Steps:

  • switch to the local branch that has relevant changes (don't pull or merge new changes from other branches)
  • rebase against (remote) branch you want to merge into (a rebase doesn't perform a merge)
    • GitLens will open an interactive UI
      • you will have the option to pick, drop, squash, edit
      • you will have the option to click on a commit hash and Open All Changes to see the diff
    • click Start Rebase and press Ctrl + Enter
  • fix conflicts
  • test and format against the newly-updated local branch
  • force push the new history
    • after a rebase the local branch will differ to the remote branch (the local branch contains the correct history)
    • overwrite commits on the remote branch, this is a destructive operation (commits that exist on the remote branch that do not exist locally will be lost)
    • the below is an example of what happens when you try and do a normal push (without force):
error: failed to push some refs to ...
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git checkout feature-branch
git rebase -i origin/main
git push --force
 
# abort rebase if needed
git rebase --abort