Skip to end of metadata
Go to start of metadata

Rebasing via the command line

Icon

Firstly, ensure that you have set this: git config --global push.default simple - otherwise a push will send ALL your local branches up to the server, and not just the one you have rebased.

  1. change working directory (cd) to the one that contains your local branch
  2. run git status to see if there are any issues

  3. run git pull to ensure everything is up to date

  4. run git rebase -i origin/staging (the -i runs the rebase in interactive mode - do not omit this) staging is the branch you are rebasing against - could be any branch, but ideally (unless you are on a sub-branch) you should rebase against the main branch

  5. You’ll now be presented with a list of your commits to rebase against. You should squash (s) all commits into the first one (even though this is the oldest commit), rewording the message (r) of that one to correctly reflect the changes within. If you don’t need to squash, or commit message is still encompassing, just pick (p) the commit to rebase

    1. Oh yeah, this is probably all in Vi, so use i to go into insert mode and change the instructions next to each commit
    2. esc then :wq (write quit) to get out of the editor and move onto the next screen

    3. i to start editing your commit message (dw will delete the word the cursor is on, if cursor is at start of word)
    4. esc then :wq (write quit) to get out of the editor and move onto the next screen
  6. git push --force will upload your local branch over the remote one. Only do this if you are rebasing your own feature branch that nobody else is working on
  7. git status to check your local and remote versions are in sync.

And we're done

Tray of mojitos

SOMETHING WENT WRONG!!!

Okay, it happens. Most likely it's a simple file conflict.

  1. run git status to get a list of problems. You're looking for files in red here - green stuff is a-okay
  2. each file listed in red will need a conflict resolved within it. Open it up in your editor and look for instances of <<<<<<< HEAD - each one is the start of a bunch of code that is conflicted
  3. everything between <<<<<<< HEAD and a line of equals signs (=================) is from the remote branch. After the equals is everything from your local version up until >>>>>>>> and your commit message
  4. Delete whichever version of the code you don't want and save the file
  5. Add each file back into the rebase using git add
  6. Run git rebase --continue to get back into rebasing, and follow step 6 onward above.