Steps below explain to keep the forked version up to date with the upstream branch of a forked repository. I know this was already documented many times, but I was struggling with it for some time, until I have found the workflow that suits me the best, so I documented it.
Create a fork in the UI, clone the forked repository and change directory:
git clone --recurse-submodules [email protected]:peterbabic/a-forked-repository.git
cd forked-repository
Add the upstream remote, do this only if remote is not already present:
git remote add upstream [email protected]:ORIGINAL-ACCOUNT/repository.git
Get back to the main branch, if not already there:
git checkout main
Fetch latest changes and add them to the repository:
git pull --rebase upstream main
This command can however fail with the following error:
CONFLICT (add/add): Merge conflict in ...
error: could not apply 11fa9d20...
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 11fa9d20...
The above means that there are local unsynchronized changes, that could be introduced via GitHub GUI for instance. To get over them, first abort the rebase:
git rebase --abort
And then run the rebase again specifying strategy:
git pull --rebase -s recursive -X ours upstream main
This command replaces local conflicting files with the files from the upstream without asking, meaning it is potentially dangerous. What is left is to push the changes back into the origin (the fork):
git push --force-with-lease origin main
Repeat last three commands to keep the forked repository updated.
Tip: It is possible to skip writing --rebase
with this setting:
git config --global pull.rebase true
For the completeness, here's how remotes should look like:
git remote --verbose
origin [email protected]:peterbabic/a-forked-repository.git (fetch)
origin [email protected]:peterbabic/a-forked-repository.git (push)
upstream [email protected]:ORIGINAL-ACCOUNT/repository.git (fetch)
upstream [email protected]:ORIGINAL-ACCOUNT/repository.git (push)
This is a 40th post of #100daystooffload.
Links
- https://github.com/tldr-pages/tldr/pull/5526#issuecomment-808965434
- https://stackoverflow.com/a/44491614/1972509
- https://stefanbauer.me/articles/how-to-keep-your-git-fork-up-to-date
- https://git-scm.com/docs/git-pull#Documentation/git-pull.txt---rebasefalsetruemergespreserveinteractive
- https://sdqweb.ipd.kit.edu/wiki/Git_pull_--rebase_vs._--merge
- https://stackoverflow.com/a/3443225/1972509