Sync Git Repository with Upstream

Sync Git Repository with Upstream

Following are the steps for syncing a forked git repository with 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.

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.

git push
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.