388

I'm used to running git pull and other commands from within a branch I'm working on. But I have set up a development server that several people work on, so I don't want to have to switch branches when I do it.

If I want to update an existing branch on the dev server from the github repository we all use, what would be the right way to do that?

If I run the command git pull github branchname will that simply pull the branch into the current branch?

All of the git examples I can find seem to indicate that you run checkout branchname first, then do the pull. I'm trying to avoid that. As I said, this is an existing branch and I just want to update to the latest version.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Diana Saunders
  • 3,889
  • 2
  • 12
  • 3
  • 10
    `git fetch` should do what you want. – Brad Sep 17 '13 at 18:47
  • 44
    `git fetch` would update the local copy of the remote branch, but not any local branch, even if one is set up to track that specific remote branch. It may or may not be what's wanted. (Edit: by default, anyway. It's possible to call it with arguments to make it behave differently, but in that case, the arguments should really be pointed out.) –  Sep 17 '13 at 18:50
  • 2
    I don't quite understand...is everyone using the same local repository on the dev server? Is that why you don't want to switch branches? Why not just have everyone make their own private clone that they can work in? See also [git: update a local branch without checking it out?](http://stackoverflow.com/questions/3216360/git-update-a-local-branch-without-checking-it-out/17722977#17722977). –  Sep 18 '13 at 03:03
  • 1
    Possible duplicate of [Merge, update, and pull Git branches without using checkouts](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) – Michael Mrozek Apr 15 '19 at 15:20

5 Answers5

528

I was looking for the same thing and finally found the answer that worked for me in another stackoverflow post: Merge, update, and pull Git branches without using checkouts

Basically:

git fetch <remote> <srcBranch>:<destBranch>

Example:

git fetch origin branchname:branchname
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
koral
  • 5,410
  • 1
  • 13
  • 5
  • 2
    Is there a way to use the upstream branch instead of specifying the source branch? – cambunctious Nov 22 '19 at 14:52
  • 1
    Sadly, but the `pull` has parameters that the `fetch` does not: `-s `, `-Xsubtree=...` which was a vital for me, so this is not an equivalent replacement. I had the issue described here: http://www.congruityservice.com/blog/fix-git-subtree-merge-wrong-directory-cannot-bind but in mine case I didn't want a checkout at all. – Andry Dec 08 '19 at 19:46
  • 23
    Considering the question is about pull, it seems the answer should instead be `git pull :`. – Woodchuck Dec 12 '19 at 20:46
  • 7
    If the commits are already in your local repository: `git fetch . origin/master:master` – Evan Apr 24 '20 at 15:32
  • 2
    If all you want is to merge (origin|online) branch into your local branch then you can use `git merge origin/master` – Abdul Rehman Oct 11 '21 at 06:36
  • If you want to pull a branch that's on remote but not local do `git fetch origin branch-name:branch-name` – Zach Saucier Apr 07 '22 at 19:20
  • What if the branches diverge? I.e. you want to do pull --rebase or just --reset? – Dan M. Jul 06 '23 at 13:08
220

I had the very same issue with necessity to commit or stash current feature changes, checkout master branch, do pull command do get everything from remote to local master workspace, then switch again to a feature branch and perform a rebase to make it up-to-date with master.

To make this all done, keep the workspace on feature branch and avoid all the switching, I do this:

git fetch origin master:master

git rebase master

And it does the trick nicely.

Marcin T.P. Łuczyński
  • 3,255
  • 1
  • 20
  • 15
  • 56
    This is good advice but buries the lede: per the below answers, if you're on `feature` and ALL you want to do is update your local `master` to be in line with origin, WITHOUT touching `feature`, just do `git fetch origin master:master`... and it's as if you did stash-checkoutMaster-pull-checkoutFeature-stashPop! – btown Jul 26 '19 at 16:25
  • 19
    To merge the origin master into your local branch, you don't need to pull the local master. You can use `git merge origin/master` – Dan Aug 16 '19 at 11:32
  • 7
    A pull is just a fetch followed by a merge/rebase, so you should be able to do the same in 1 line: `git pull origin main:main --rebase` – jor Jan 15 '21 at 06:39
5

I used following command to update origin branch to local branch without checkout. git fetch origin [BranchName]:[BranchName]

Piyush
  • 1,156
  • 12
  • 20
1

If you want the local branch tips to get re-pointed after git fetch, you need some additional steps.

More concretely, suppose the github repo has branches D, B, C, and master (the reason for this odd branch-name-set will be clear in a moment). You are on host devhost and you are in a repo where origin is the github repo. You do git fetch, which brings over all the objects and updates origin/D, origin/B, origin/C, and origin/master. So far so good. But now you say you want something to happen, on devhost, to local branches D, B, C, and/or master?

I have these obvious (to me anyway) questions:

  1. Why do you want the tips of all branches updated?
  2. What if some branch (e.g., B) has commits that the remote (github) repo lacks? Should they be merged, rebased, or ...?
  3. What if you're on some branch (e.g., C) and the work directory and/or index are modified but not committed?
  4. What if the remote repo has new branches added (A) and/or branches deleted (D)?

If the answer to (1) is "because devhost is not actually for development, but rather is a local mirror that simply keeps a locally-available copy of the github repo so that all our actual developers can read from it quickly instead of reading slowly from github", then you want a "mirror" rather than a "normal" repo. It should not have a work directory, and perhaps it should not accept pushes either, in which case the remaining questions just go away.

If there is some other answer, (2-4) become problematic.

In any case, here's a way to tackle updating local refs based on remote refs (after running git fetch -p for instance):

for ref in $(git for-each-ref refs/remotes/origin/ --format '%(refname)'); do
    local=${ref#refs/remotes/origin/}
    ... code here ...
done

What goes in the ... code here ... section depends on the answers to questions (2-4).

torek
  • 448,244
  • 59
  • 642
  • 775
-16

Use

git fetch

instead. It updates the remote refs and objects in your repo, but leaves the local branches, HEAD and the worktree alone.

SzG
  • 12,333
  • 4
  • 28
  • 41
  • 18
    But that doesn't update the local branches of his development server... it only refreshes the "origin" branches on that git folder, which correspond to the asker's github repo. – ANeves Oct 03 '17 at 10:52
  • 1
    Or, if you want my version of the problem: I want to merge my working branch with "master", and not with "origin/master of ssh://bla bla bla". Doing fetch will update origin/master, but not master. – ANeves Oct 03 '17 at 10:53