How to update a Git branch from the main or master branch

; Date: Tue Feb 18 2025

Tags: Git »»»»

It's recommended to work on bugs or features in a separate Git branch from the main branch. In a long-running development task, the branch may fall behind the main branch. What we'll do in this article is learn how to bring the development branch up-to-date with the main branch to ease committing it to main.

In a typical software engineering project, the main (or master) branch will contain the "latest" version of the software. Development of new features or bug fixes should occur on a separate branch. The engineer in charge of that branch then creates a pull request to merge that branch into the main branch.

I found myself in this case and uncertain how to proceed. The main branch had changes blocking the pull request from being merged to main. While I've been using Git for many years, I've not had reason to delve through all the magickal incantations available in the git command. This task, merging from main to a feature branch, was not something I'd had to do previously.

I'm sure many people are in the same boat. Day-to-day use of Git leaves us comfortable with git pull, git push, and the like. With GitHub, the task of creating a pull request is easy because GitHub guides one through the process; step one is to create a branch (git checkout -b branch_name), then you can push the branch to the shared repository (git push -u origin branch_name), at which point GitHub gives you a URL to visit to create a pull request.

This is a rough geometric description of the repositories.

The main branch of the remote repository could easily be many commits ahead of the local main branch (local_main). What Git commands can we run to prepare local_branch_name to push to remote_branch_name and then be merged into remote_main?

The first command to learn is how to merge from one branch to another:

$ git checkout branch_name
$ git merge main

This merges commits into the current branch from the named branch. In this case, it is merging from main to the currently checked-out branch. If there's nothing to update, it'll print: Already up-to-date. In my case an editor window popped up to edit the commit message for the merge.

The result is that local_branch_name will be updated from local_main.

But, if remote_main is many commits ahead of local_main, then local_branch_name will also be many commits behind remote_main. It's desired that local_branch_name be up-to-date with the commits in the remote_main branch on the remote Git repository.

There are two ways to proceed to update the local branch from the remote main branch on the remote Git repository.

##### Option 1

$ git checkout main
$ git pull
$ git checkout branch_name
$ git merge main

This option first updates the local Git repository from the remote. It then merges updates from local_main to the local_branch_name.

##### Option 2

$ git checkout branch_name
$ git merge origin main

This directly pulls the updates from the main branch on the remote Git repository into the local branch (local_branch_name).

H/T (stackoverflow.com) https://stackoverflow.com/questions/41045548/merging-changes-from-master-into-my-branch

Summary

It turns out that updating a local branch from the remote main branch is rather easy. No incantaions of high Git Magick are required, and it's very easy for us mere mortals to learn.

Running git merge --help shows there are close to a zillion options. Therefore, this has taken you to the doorway through which you can learn some high Git Magick if that's what you desire. If you like, someone is holding out a red pill and a blue pill. You can take the red pill and explore the depths of Git Magick. Me? I'm happy having learned this little bit and go back to developing my software.

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.