How to keep a branch in sync with the main branch in Git?

October 19, 2020 - 2 min read

To keep the local git branch in sync with the remote main branch first, you need to get the latest commits from the remote main branch.

Note: main is the new name for master branches if you are using Github 🌟.

So first, If you are in the branch where you want the latest commits. Then you need to change to your local main branch. This can be done using this command,

git checkout main

Then you need to pull or get the latest commits from the remote main branch. This can be achieved using this command,

git pull main

After that, you can change to the branch you need to sync the main branch using this command,

git checkout your_branch_name

Then all you have to do is merge the latest commits from the main branch we just fetched.

This can be done using this command,

git merge main

That's it. You are done.

Sometimes you may get merge conflicts during the process, there you need to decide upon which changes you need to keep and which changes to be removed.

Feel free to share if you found this useful 😃.