Skip to content
Hans Kristian Rosbach edited this page May 2, 2023 · 5 revisions

Git rebase (Pull in latest changes from 'develop' to your branch)

If you have already pulled the latest 'develop' changes (into your local 'develop' that is), then you can do a simple:

# First make sure develop branch is up-to-date
git remote update
git checkout develop
git pull

# Now switch to your branch and rebase it on top of the updated develop branch
git checkout branchXY
git rebase develop

What rebase does:

  • Rolls back your branch 'branchXY' to a point in common with 'develop'
  • Pulls in the new commits from 'develop'
  • Re-applies 'branchXYs' commits on top without making a merge commit.

If there are conflicts during rebase, you might have to resolve these manually.
After the rebase, you will need to do a force push to update a remote repo like github, but as long as it is a PR, nobody should really care much about the commit log when doing a rebase anyway.
This method can make keeping multiple branches in sync with a main branch easier and cleaner.

Git fixup (Change the contents of a previous commit)

This lets you add changes to any previous commit, not just the last one (for that, use git commit --amend instead).

git add <your fixed files>
git commit --fixup=OLDCOMMIT
git rebase --interactive --autosquash OLDCOMMIT^

OLDCOMMIT is the hash of the commit you want to change/fix
What this does:

  • Makes a new commit at head
  • Rolls back
  • Squashes OLDCOMMIT with the new commit
  • Rebases the rolled-back commits on top of the new fixed commit

If there are conflicts during rebase, you might have to resolve these manually. After the rebase, you will need to do a force push to update a remote repo like github. But it is great when you are working on a PR where you want to make multiple commits for ease-of-review or to separate changes, because you just know you'll need to modify those previous commits just as soon as you have made another after it. (I recommend experimenting a little with this one before using it for real, just to get a feel for how it actually works).