Skip to content

How to rebase

SpechtMagnus edited this page Nov 18, 2019 · 1 revision

How To Rebase

Rebase Local Commits on Remote Branch

git pull --rebase

Use this command for every pull or set your default pull behavior to rebase. This can be achieved via

git config --global pull.rebase true

Or simply add an alias to your ~/.bashrc (Linux only)

Rebase Local and Remote Branch onto Master

git fetch
git rebase origin/master

After this the history of the local branch will be different from the history of the remote branch so you have to push with force. Use force-with-lease to only override known changes. (i.e. avoid overriding changes that someone pushed after your last pull/fetch)

git push --force-with-lease

Integrate remote branch after someone else rebased the branch you're working on onto master

git fetch origin
git reset --hard

WARNING This will remove all commits that were not on the remote branch!

To save your local commits you have to rebase them onto the remote branch using the last pushed commit.

git tag temp-base [ID of the last commit that has already been pushed]

git fetch
git rebase temp-base [branch-name] --onto origin/[branch-name]

git tag -d temp-base