Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

https://www.educative.

io/answers/how-to-pull-a-branch-from-origin-in-github

GitHub contains fixed commands to perform various tasks and actions.

To include a project from GitHub to a local repository, we must write the git
pull command.

Why would we pull a remote branch?

We pull a remote branch as it downloads new changes from branchname onto the remote
branch named origin and integrates them into our local branch.

branches in git
Branches are the different realities of a master branch. We can create several branches
and merge them with our primary working branch, called the master branch.

Syntax

We can use git pull as an alternative to git fetch and git merge in the same command.

Git does not merge the changes from the branches into our current master. But, suppose
we’ve checked out the branch master, and now we want to merge in the remote
branch branchname.

To integrate this command into our project, we write it in the following way:

git pull origin branchname

Explanation

The origin is the remote branch which is the primary working directory of a project. All
other branches merge into this branch.

is just another branch, or a copy of the original branch, where developers code
branchname
independently. And after the final review from testers, these local branches merge with
the master branch origin.

When we write the above command, git is applying two commands:

git fetch origin branchname && git merge branchname

Purpose of the git pull origin branchname command

The git pull origin branchname generally tells the git to pull/fetch (projects and data) from the remote
repo named origin to our branchname, illustrated in the figure below.
Git Pull vs Fetch

You’re likely in the following situation: there were changes recently made to your remote
repository and you want to incorporate them into your local copy. You have a few options
here; the two most common actions to get changes from your remote are Git pull and Git
fetch.
So, what’s the difference between Git pull vs fetch, and when should you utilize which
command? We’re glad you asked.

What is Git fetch?

Git fetch is a command that allows you to download objects from another repository.

What is Git pull?

Git pull is a command that allows you to fetch from and integrate with another repository or
local branch.

From this definition, you can see that a Git pull is actually a Git fetch followed by an additional
action(s)—typically a Git merge.

https://www.git-tower.com/learn/git/faq/difference-between-git-fetch-git-pull
What's the difference between git fetch and git pull?

Before we talk about the differences between these two commands, let's stress their
similarities: both are used to download new data from a remote repository.

Downloading data is an essential step in your daily work - because the remote data you are
looking at in your local repository is just a "snapshot". It's only as up-to-date as the last time
you explicitly downloaded fresh data from the remote with "fetch" or "pull". It's vital to keep
this fact in mind when inspecting remote branches and commits!

Let's now look at the fine but important differences between "fetch" and "pull".

Fetch

$ git fetch origin

git fetch really only downloads new data from a remote repository - but it doesn't integrate
any of this new data into your working files. Fetch is great for getting a fresh view on all the
things that happened in a remote repository.

Due to it's "harmless" nature, you can rest assured: fetch will never manipulate, destroy, or
screw up anything. This means you can never fetch often enough.

Pull

$ git pull origin master

git pull, in contrast, is used with a different goal in mind: to update your current HEAD
branch with the latest changes from the remote server. This means that pull not only
downloads new data; it also directly integrates it into your current working copy files. This
has a couple of consequences:

 Since "git pull" tries to merge remote changes with your local ones, a so-called "merge
conflict" can occur. Check out our in-depth tutorial on How to deal with merge
conflicts for more information.

 Like for many other actions, it's highly recommended to start a "git pull" only with a
clean working copy. This means that you should not have any uncommitted local changes
before you pull. Use Git's Stash feature to save your local changes temporarily.

You might also like