327

I have cloned a git repository to my dev server and then switched to the dev branch but now I can't do a git pull to update the branch.

How do I update the code on the server ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Alex
  • 6,205
  • 7
  • 43
  • 53
  • A beginner's remark: I tried to find a question about whether I can use `git pull my_branch` for `git fetch my_branch + git checkout my_branch` and it turns out that this throws `fatal: my_branch does not appear to be a git repository \n fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.` You can only use `pull` if you want to merge another branch into the already checked out branch or if you want to update an already checked out branch. – questionto42 Jul 20 '22 at 12:15

10 Answers10

442

See the git-pull man page:

git pull [options] [<repository> [<refspec>...]]

and in the examples section:

Merge into the current branch the remote branch next:

$ git pull origin next

So I imagine you want to do something like:

git pull origin dev

To set it up so that it does this by default while you're on the dev branch:

git branch --set-upstream-to=origin/dev dev
Community
  • 1
  • 1
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 10
    "`--set-upstream` flag is deprecated and will be removed. Consider using `--track` or `--set-upstream-to Branch dev` set up to track remote branch dev from origin." Eg. `git branch --set-upstream-to origin/dev` – Nagendra Rao Jul 03 '15 at 05:19
  • Edit this to reflect deprecation of --set-upstream? – Scott Skiles Dec 06 '18 at 17:52
  • Personally I am interested in the flow when I want to pull one specific branch from origin that hasn't been fetched yet and put those object on separate branch, so my current branch is not merged with what I'm pulling. – Yuri Pozniak Nov 01 '19 at 20:37
62

Here is what you need to do. First make sure you are in branch that you don't want to pull. For example if you have master and develop branch, and you are trying to pull develop branch then stay in master branch.

git checkout master

Then,

git pull origin develop
thestar
  • 4,959
  • 2
  • 28
  • 22
  • 20
    This is confusing. If I want to pull a remote "dev" branch into a local "dev" branch, I would think that you need to stay in the local "dev" branch and not any other branch. – Roman Nov 05 '17 at 12:37
  • 8
    Doesn't this fetch the remote develop branch, and then merge it with your local master? – Brad P. Apr 25 '18 at 15:56
  • 4
    I did `git checkout develop` and `git pull origin develop` and worked well. You would need to checkout other branch if you want to remove a local or remote branch (`git branch -d develop` or `git push origin --delete develop`) – ChesuCR Jan 03 '19 at 16:46
  • 2
    very confusing. I just merged the branch I wanted to test into master locally :) – Fabian Bosler Nov 22 '19 at 07:17
  • As @roman says, one needs to be in the local branch. – woter324 Jun 18 '20 at 21:58
56

if you want to pull from a specific branch all you have to do is

git pull 'remote_name' 'branch_name'

NOTE: Make sure you commit your code first.

Ali Hassan
  • 779
  • 10
  • 14
19

It's often clearer to separate the two actions git pull does. The first thing it does is update the local tracking branc that corresponds to the remote branch. This can be done with git fetch. The second is that it then merges in changes, which can of course be done with git merge, though other options such as git rebase are occasionally useful.

wnoise
  • 9,764
  • 37
  • 47
17

Here are the steps to pull a specific or any branch,

1.clone the master(you need to provide username and password)

       git clone <url>

2. the above command will clone the repository and you will be master branch now

       git checkout <branch which is present in the remote repository(origin)>

3. The above command will checkout to the branch you want to pull and will be set to automatically track that branch

4.If for some reason it does not work like that, after checking out to that branch in your local system, just run the below command

       git pull origin <branch>
vikas etagi
  • 625
  • 1
  • 13
  • 15
15

Laravel documentation example:

git pull https://github.com/laravel/docs.git 5.8

based on the command format:

git pull origin <branch>

Erich García
  • 1,648
  • 21
  • 30
9

This worked perfectly for me, although fetching all branches could be a bit too much:

git init
git remote add origin https://github.com/Vitosh/VBA_personal.git
git fetch --all
git checkout develop

enter image description here


If you want to avoid the overkill of pulling everything and only getting the explicit branch (in this case 'develop'), then the following works:

git init
git remote add origin https://github.com/Vitosh/VBA_personal.git
git fetch origin develop
git checkout develop
Vityata
  • 42,633
  • 8
  • 55
  • 100
6

You can take update / pull on git branch you can use below command

git pull origin <branch-name>

The above command will take an update/pull from giving branch name

If you want to take pull from another branch, you need to go to that branch.

git checkout master

Than

git pull origin development

Hope that will work for you

Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
2

git-pull - Fetch from and integrate with another repository or a local branch

git pull [options] [<repository> [<refspec>...]]

You can refer official git doc https://git-scm.com/docs/git-pull

Ex :

git pull origin dev

Sesha
  • 189
  • 1
  • 4
  • How is your answer any different from any of the others already posted? – j08691 Dec 16 '19 at 16:29
  • Yeah, nothing much only thing is official docs ( https://git-scm.com/docs/git-pull ) so you can explore options lot. – Sesha Dec 17 '19 at 15:55
0

in my case, Devop was updated(or HEAD) and Main was older(like 20 commits back), using git fetch --all and --allow-unrelated-histories, works perfectly

like this:

git clone https://github.com/xxxxxxxx
git remote add origin https://github.com/xxxxxxxx
git fetch --all

(here,i got a merge unrelated histories error,so)

git pull origin devop --allow-unrelated-histories

(at this point i got some conflicts that solved with a new commit)

and finally

git push -u origin devop
nativelectronic
  • 646
  • 4
  • 11