Skip to content

Instantly share code, notes, and snippets.

@curquiza
Last active March 5, 2023 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save curquiza/5f7ce615f85331f083cd467fc4e19398 to your computer and use it in GitHub Desktop.
Save curquiza/5f7ce615f85331f083cd467fc4e19398 to your computer and use it in GitHub Desktop.
Git rebase from a forked repository

Rebase from a forked repository

Add the main repository as a remote

Remotes represent the URLs of Git repositories, e.g. origin.

Make sure you have the main repository as a remote:

$ git remote -v
origin	git@github.com:curquiza/MeiliSearch.git (fetch)
origin	git@github.com:curquiza/MeiliSearch.git (push)

If not, add the main repository as a new remote:

$ git remote add upstream git@github.com:meilisearch/MeiliSearch.git
$ git remote -v
origin	git@github.com:curquiza/MeiliSearch.git (fetch)
origin	git@github.com:curquiza/MeiliSearch.git (push)
upstream	git@github.com:meilisearch/MeiliSearch.git (fetch)
upstream	git@github.com:meilisearch/MeiliSearch.git (push)

⚠️ This example works for the MeiliSearch repository: don't forget to replace MeiliSearch by the repository name on what you are working!

Rebase

The goal of the rebase is to move your commits (from your branch) to be placed right after the last commit on main, keeping the Git history clear. See this documentation for more details about the rebase.

Make sure you are on main and your local commits on main are up-to-date with the upstream ones:

$ git checkout main
$ git pull upstream main

Go back to your branch and rebase:

$ git checkout your-branch-name
$ git pull origin your-branch-name # In case you accepted commit suggestions on the GitHub interface
$ git rebase main

You might have some merge conflicts. Fix them (git status could help you about what to do).

Merge conflicts or not, you finally have to do:

$ git push --force origin your-branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment