Rebasing via the command line
- change working directory (cd) to the one that contains your local branch
run git status to see if there are any issues
run git pull to ensure everything is up to date
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
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
- Oh yeah, this is probably all in Vi, so use i to go into insert mode and change the instructions next to each commit
esc then :wq (write quit) to get out of the editor and move onto the next screen
- i to start editing your commit message (dw will delete the word the cursor is on, if cursor is at start of word)
- esc then :wq (write quit) to get out of the editor and move onto the next screen
- 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
- git status to check your local and remote versions are in sync.
And we're done
SOMETHING WENT WRONG!!!
Okay, it happens. Most likely it's a simple file conflict.
- run git status to get a list of problems. You're looking for files in red here - green stuff is a-okay
- 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
- 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
- Delete whichever version of the code you don't want and save the file
- Add each file back into the rebase using git add
- Run git rebase --continue to get back into rebasing, and follow step 6 onward above.