Git: Update a feature branch with remote main branch

Update a git branch with remote main branch

When our feature git branch is behind main branch, we need to bring feature branch up to date with main branch before raising a PR to merge feature into main branch.

Updating Feature Git branch with main branch

Step-1: first commit your changes to your development branch and checkout to local main branch.

$ git checkout main

Step-2: fetch the latest branches and their commits from remote repo.

$ git fetch -p origin

Step-3: Merge the changes from origin/main into your local main branch.
The below command ‘sync’s your main branch with remote repo, without losing your local changes

$ git merge origin/main

Step-4: Check out the branch you want to merge into.

$ git checkout <dev branch name>

Step-5: Merge your recently updated local main branch into your development branch to update it.

$ git merge main

The above command will open a text editor. add a commit message, save, and quit.

Once done, your local development branch will get updated with changes from remote main branch.

Step-6: to update your remote development branch run below command

$ git push origin <dev branch name>

Conclusion

In this quick demo we have successfully updated a feature branch with remote main branch. You can find more information in Git official documentation.

For more on Git:

Git: Squash commits

Leave a Reply

Discover more from DevOps Counsel

Subscribe now to keep reading and get access to the full archive.

Continue reading