Following are the steps for syncing a forked git repository with upstream:
- Add remote for upstream repository. Use the
git cloneURL from upstream:
git remote add upstream <upstream_repositery_link>.git
Here we added a remote, naming it upstream (can be varied). Remotes are set of tracked repositories. You can view all the remotes for present git repository by doing
git remote -v.
- Fetch changes from upstream and merge/sync in local repository:
git fetch upstream git checkout master git merge upstream/master
Note - Replace master with main if your repository has switched to naming the default branch as main.
git fetch is used to fetch remote/upstream changes.
git checkout is used to switch between branches.
git merge is used to merge changes from different remotes/branches.
- Push these changes to your hosted git repository (origin) on GitLab/GitHub/SourceHut/Codeberg which will also sync it with upstream:
- (optional) To sync all the changes from master/main into feature branch:
git checkout <branch_name> git merge master
PS: Work via branches to avoid merge conflicts. New branches can be created via
git checkout -b <branch_name>. Use
git checkout master to switch back to master branch.