. Advertisement .
..3..
. Advertisement .
..4..
This guide will show you how to pull changes from a specific branch in Git. With the right command, you can fetch content from a remote branch and update your local branch to match it. If you are more familiar with GitHub Desktop, there is also a GUI workflow for you.
Pull Changes From A Specific Branch In Git
Branch Synchronization In Git
You can keep the remote repository and your local brand in sync by fetching new commits and merging them with your local branch. It involves every change from the point where the two branches diverged. This task makes your copy of code up to date with the central repository when there are multiple contributors to the project.
Unlike pushing, which updates a branch on the remote repository, you only need to update your own copy of the branch.
Pull From A Branch With Git
You don’t need a special git command to get the job done. You can use git pull with the correct options for this task.
We will use a project on GitHub as an example. First, we need to clone the remote repository first:
$ git clone https://github.com/org-roam/org-roam.git
When done, you will have a copy of the repository’s master branch on your system. If you run into the “Could not read from remote repository” error, follow this guide to solve it.
Suppose you need to check for commits on the remote branch and merge with your local brand, git pull is the command you need.
Most of the time, we can use it without any options. In this case, Git uses the default remote and branch set by the current configurations.
$ git pull
But this command only affects the default branch (master) by default. To specify a branch to merge commits from, you will use this syntax:
$ git pull <remote> <branch>
In which <remote> is the remote repository you want to check for new commits, and <branch> is the name of the branch that Git needs to compare and fetch updates for you.
This command will pull changes from the gh-pages branch on org-roam’s GitHub repository and merge them to your local brand:
$ git pull origin gh-pages
As you may already know, origin is the shorthand for the repository from which you have cloned your local copy.
Remember that you can use the –rebase option to rebase your branch instead of simply merging commits.
$ git pull --rebase origin gh-pages
The rebasing operation rewrites the whole history of your local copy by creating new commits for every commit of your existing branch. A big benefit of this option is a cleaner history. Rebasing will remove unnecessary commits that are usually created by the merge option. You will have a linear history, allowing you to follow the branch throughout history without forks.
But rebasing has some potential pitfalls you may want to avoid too. Rewriting the history of your branch can be risky, reducing traceability and removing some necessary contexts provided by a normal merge commit.
Pull From A Branch On GitHub
When the project is located on GitHub, and you use its desktop application to manage your work, you can use this GUI tool instead of the command-line interface.
- Open GitHub Desktop and select your local brand that needs to be updated in the Current Branch drop-down.
- Click Fetch origin to check for new commits since the last time you synced your local branch with the remote branch.
- To pull changes to your local branch, click Pull origin. If you want to rebase, click Pull origin with rebase.
- If there are merge conflicts, resolve them with your preferred tool.
Summary
You pull changes from a specific branch in Git with the git pull command. Since the default brand doesn’t apply anymore, you will need to specify the branch name to pull changes from the correct remote branch.
Leave a comment