9

I know there are other questions that address variants to this question. Namely this one.

The issue with that is that the answers assume that all local branches are already tracking some remote branch (or even exist remotely).

The issue I am facing now is that I am working with a remote team, and some times they may create a remote branch that I don't have locally.

How do I, in 1 command, pull both the latest changes to the branches I am tracking locally and also pull the latest version of all the branches I am not tracking.

When I do git pull --all that simply pulls the latest of my tracked branches.

Thanks.

Edit:

I have two remotes - bitbucket and heroku.

I want to pull all the branches from my bitbucket remote....not from my heroku remote.

I did this:

$ git pull bitbucket --all fatal: fetch --all does not take a repository argument

And this:

$ git pull --all bitbucket fatal: fetch --all does not take a repository argument

Also this:

$ git pull bitbucket Password: You asked to pull from the remote 'bitbucket', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.

Community
  • 1
  • 1
marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • Please update your question with more details about what you want (which remotes you do or do not want to fetch from), what commands you have tried so far, and what were the results. –  Jul 19 '13 at 23:47
  • @Cupcake I have done that. – marcamillion Jul 20 '13 at 03:11

3 Answers3

5
$ git remote update
$ git pull --all

should do it - though its not 1 command

exussum
  • 18,275
  • 8
  • 32
  • 65
  • The issue is I have 2 remotes. Heroku and bitbucket. How do I factor that in? When I do `git remote update` the first remote it checks is Heroku - which I don't want it to do. – marcamillion Jul 19 '13 at 21:15
  • `git remote update bitbucket` should get the bitbucket one up to date – exussum Jul 19 '13 at 21:24
  • 1
    How do I specify to pull from `bitbucket`? `$ git pull bitbucket --all fatal: fetch --all does not take a repository argument` is what happens. If I just do `git pull --all` it tries to pull from `heroku` first. – marcamillion Jul 19 '13 at 21:43
  • its `git pull --all bitbucket` use man git-pull to see more info – exussum Jul 19 '13 at 21:46
  • 1
    Yeh...I tried that. It didn't work `$ git pull --all bitbucket fatal: fetch --all does not take a repository argument` – marcamillion Jul 19 '13 at 21:51
  • Just `git pull bitbucket` should pull all the branches. – Sailesh Jul 19 '13 at 21:55
  • 1
    `$ git pull bitbucket Password: You asked to pull from the remote 'bitbucket', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.` – marcamillion Jul 19 '13 at 22:20
1

Updated answer based on new information from original poster regarding which remote he wants to fetch from:

git fetch bitbucket

will update your remote tracking branches as well as add new ones if there are new branches on the remote.

fetch won't update your local branches unless you also pass it a refspec, and it will only do a fast-forward or forced update in such cases, not a true merge.

Here is an alias that will fetch from bitbucket and update both your remote-tracking branches and your local master branch:

sync = "!git checkout --quiet head && git fetch --prune bitbucket +refs/heads/*:refs/remotes/bitbucket/* master:master && git checkout --quiet - || git checkout --quiet -"

You use it like this: git sync. Done!

The --prune option to fetch means it will also remove obsolete remote-tracking branches of branches that no longer exist in your bitbucket remote.

The refspec +refs/heads/*:refs/remotes/bitbucket/* means to fetch all branches from bitbucket, and map them to our remote-tracking branches for bitbucket in your local repo. The + in front means it will allow forced-updates, which is generally okay for your remote-tracking branches.

The refspec master:master means takes the master branch on bitbucket, and use it to update your local master branch. Since there is no +, it won't do a forced update. If you want to allow that, add a + in front, like this: +master:master.

The git checkout --quiet head is necessary to do first if you happen to have your local master checked out, because otherwise fetch won't update it while it's checked out. If you happen to have it checked out when you run the command, it will checkout the commit you're directly on instead (that's what head means), so that the master branch label is free to move while you're not on it.

The git checkout --quiet - || git checkout --quiet - checks back out whichever branch you were on before git checkout --quiet head was run, regardless of whether or not the fetch and update of branches worked. (I've actually had problems in the case of failure actually, and I'm not sure why...if you end up in detached head state, just run git checkout <branch name> and you'll be okay).


Old answer:

If you use git fetch --all, that will fetch all the branches from all the remotes that you've added to your repo. It won't update all of your local branches, but it's a start.

  • 1
    I'm in the "you should never ('what, never?' 'well, hardly ever') use pull anyway" camp myself, so I consider that a feature. :-) – torek Jul 19 '13 at 23:42
  • 1
    Well I don't want to pull all the branches from all the remotes. I just want to pull all the branches from 1 remote. How do I do that with `git fetch`? – marcamillion Jul 20 '13 at 03:07
  • @marcamillion `git fetch ` should give you what you want. –  Jul 20 '13 at 03:14
  • @marcamillion are you interested in just updating your remote-tracking branches and fetching new remote-tracking branches, or did you also want to update branches that are local to your repo? –  Jul 20 '13 at 03:38
  • 1
    I wanted to basically do a pull (which is a fetch & merge/rebase if the flag is supplied) on `bitbucket, that will both update my local branches with all the changes on their remote equivalents, and will also fetch any new branch just created that I don't have locally. – marcamillion Jul 20 '13 at 21:42
0

git fetch all
is also a non intrusive option.
forvaidya
  • 3,041
  • 3
  • 26
  • 33