Skip to content

Instantly share code, notes, and snippets.

@JorgenEvens
Last active November 7, 2015 10:51
Show Gist options
  • Save JorgenEvens/3da05f14d0f5a6bbc2bd to your computer and use it in GitHub Desktop.
Save JorgenEvens/3da05f14d0f5a6bbc2bd to your computer and use it in GitHub Desktop.
Sync a pull request with master without a merge commit.

Rebasing a pull request

First of all, use this with care this can destroy your work and only when one of the core developers asks you to do so.

This will show how to rebase a pull request to ensure it is up to date with current master For this we will assume that upstream is the name of the git remote that points to the bubobox repository. To set up the remote branch check out the section on setting up the upstream branch. If you are looking on how to pull a branch that has been rebased check here.

First ensure that you are on the branch of your pull request and not on some other branch.

git checkout feature/my-awesome-feature

Next we will create a backup of our branch just in case something goes wrong during the rebase.

git checkout -b backup/my-awesome-feature

Now change back to the branch of your pull request

git checkout feature/my-awesome-feature

Now it is time to update the information we have about the upstream branches

git fetch upstream

Now we are ready to rebase our branch

git rebase upstream/master

If there are no conflicts this will rebase your branch and should output a message that says: In case there are conflicts you should have a look at rebasing with conflicts.

First, rewinding head to replay your work on top of it...
Applying: commit 1
Applying: commit 2
...
Applying: commit 5

At this point you can push your local branch back to the pull request, however you will have to force push this branch so be careful. To ensure I have the correct branch I usually first attempt to push without the --force flag to ensure I have the correct branch. Without the force flag you should receive an error similar to the one below.

git push origin feature/my-awesome-feature
To git@github.com:bubobox/components.git
 ! [rejected]        tmp -> feature/my-awesome-feature (non-fast-forward)
error: failed to push some refs to 'git@github.com:bubobox/components.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Once we have verified that we are pushing to the correct branch we can use the --force flag to overwrite the current history of the pull request. The output of this should look like a regular git push.

git push origin feature/my-awesome-feature --force

Once you verified that everything is still intact you can remove the backup branch we created earlier

git branch -D backup/my-awesome-feature

Should anything have gone terribly wrong you can restore your old branch again with the commands below

git checkout backup/my-awesome-feature
git branch -D feature/my-awesome-feature
git checkout -b feature/my-awesome-feature
git branch -D backup/my-awesome-feature

upstream branch

To setup the upstream branch execute the following command, where you replace <repo> with the name of the repository for which you want to set up the upstream.

git remote add upstream git@github.com:bubobox/<repo>.git

Rebasing with conflicts

Git will automatically re-apply all your patches on the current master branch, so you could receive conflicts on any of the commits you have on your branch. Git will automatically stop when it detects such a conflict. Similarly you can then fix the conflicts by editing the files in your working directory. Once the conflicts are resolved you should add the updated files and tell git to resume rebasing.

git add .
git rebase --continue

Should you want to abort the rebase operation at any time you can always do so using:

git rebase --abort

This command will return your branch to the original state.

Pull rebased branch

In order to pull changes from a branch / pull request that has been rebased by someone else you have to delete your own local branch and checkout out the remote branch again.

First create a backup of your current branch

git checkout feature/my-awesome-feature
git checkout -b backup/my-awesome-feature

Next delete your local branch and check out the updated branch

git branch -D feature/my-awesome-feature
git fetch origin
git checkout feature/my-awesome-feature
@Sitebase
Copy link

Sitebase commented Nov 7, 2015

Great explanation of how to use rebase @JorgenEvens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment