15

What does git fetch origin master:master mean?

I mean the master:master part: I know what git fetch origin means, but what about the master:master part?

Will
  • 24,082
  • 14
  • 97
  • 108
Mahdi
  • 1,871
  • 2
  • 24
  • 41

1 Answers1

19

The arguments after the remote (origin) are refspecs.

Using master:master will overwrite your master branch; see this answer.

See this answer for even more about git fetch's behavior with and without refspec arguments.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • Maybe it is not the right place to ask, but it also seems like something to not make a separate question for it. Does `git fetch origin branch:branch` do exactly the same as `git pull` called on `branch`? – C. Binair Feb 26 '21 at 13:09
  • 1
    @C.Binair: no, these are—at least potentially—very different, because `git pull` runs *two* commands. The first one is a `git fetch` that acts a lot like `git fetch origin branch:origin/branch`, with the weasel-wording ("acts a lot like") just to handle uncommon special cases. The *second* command that `git pull` runs is up to you: you can have it run `git merge`, or `git rebase`. Both of these are potentially quite different from what `git fetch` will do as both `git merge` and `git rebase` can create *new* commits. Fetch cannot: fetch can only obtain *existing* commits from some other Git. – torek Feb 26 '21 at 16:28
  • 1
    @C.Binair: The tricky part here is that both `git merge` and `git rebase` themselves have several special cases where they *don't* need to create new commits, and thus don't bother. **When that happens** the kind of `git fetch` you ask about *can* do the same job. That's because no new commits were needed. Fetch can't create *new* commits, but if the job is simple enough, fetch can do it. It's like having your dog retrieve shoes: if they're where they should be, that works. If it requires going to the shoe store and buying *new* shoes, well, that's asking too much. :-) – torek Feb 26 '21 at 16:31
  • Would ` git fetch --all` covers `git fetch origin master:master ` ? – alper Jul 07 '21 at 21:59
  • @alper: no. The `--all` flag to `git fetch` means *all remotes*. The `master:master` argument to `git fetch origin master:master` is a *refspec*, and refspecs must be provided to override the defaults that `--all` would use for each remote. – torek Jul 08 '21 at 01:14
  • A quick question (and if it turns out not to be quick I'll post it as a new question). Is there a way to do the equivalent of `git fetch origin master:master` without giving the branch name twice? I often do `get fetch origin branch:branch`; I rarely if ever do `git fetch origin branch:different_name`. – Keith Thompson Sep 10 '21 at 20:22
  • 1
    @KeithThompson: other than with an alias or script, no. Obviously trivial to do with a script (or shell alias). Git aliases are weird and messy so I'd use a Git alias that invokes a shell function. – torek Sep 10 '21 at 21:32