Update Local Branch From Remote in Git

  1. Remote Branches in Git
  2. Update Feature Branch in Git

Git is a popular and well-known platform for developers and their development teams. We can say that working with Git is crucial because it gives many unique features compared to others.

For developers, it’s essential to know how to handle its functionalities, such as Git repositories and the respective functions to pull and push, etc.

We faced various situations while working with Git when we had to update our local branch from the remote branch so that we should be up to date for the current teamwork.

Remote Branches in Git

Before we get into this topic, we should know somewhat about remote branches. Remote branches are referred to as branches present in a remote repository and can be accessed with a few Git commands.

A remote-tracking branch is a branch in your local repository that tracks the remote branch. It’s the branch where each team member commits their work so that everyone can pull and maintain the latest up-to-date local branch.

Update Feature Branch in Git

Let’s suppose that our feature (local branch) isn’t up-to-date yet, and now we have to fetch changes from our master (remote) branch so that our local feature branch gets updated from the latest updates.

First of all, we will update our master branch. For this purpose, we go to our local project and check out the branch we want to merge into our master branch.

We will run the following command.

$ git checkout master

We will fetch the remote branch by bringing the branches and their commits from the remote repository. The option -p full form is --prune, used to delete remote-tracking references not present in the remote.

Commits to master will be preserved in a local branch: remotes/master/original.

$ git fetch -p origin

Now we will merge the changes that we want from the origin into our local branch. Our master branch will sync with the remote repository without losing our local changes.

We will run the following command for merging the remote branch to the local.

$ git merge origin/master

If some changes in the local master branch are not available in the remote origin/master branch, we will use the git pull here so that the latest changes will be pulled.

The git pull command will apply merge and build a merge commit that combines those changes.

$ git pull

We will check out the branch that we want to merge into.

$ git checkout <feature-branch>

We will now merge our master branch into our local feature branch so that it gets updated with the latest changes from our team.

$ git merge master

This method will only update our local feature branch. To update it on the remote branch, we will push the changes that we made, and then all the updated local changes will be pushed to the remote branch.

$ git push origin <feature-branch>
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Git Update

  • Update Branch From Master in Git