Search code examples
gitgit-fetch

git difference between fetch branch vs fetch branch:branch


I am confused about

git fetch origin master vs git fetch origin master:master.

I know first will fetch from repository and specified branch.

what about second git fetch origin master:master?


Solution

  • The argument you're varying in these examples is called the refspec.

    In the first example, you're using a shorthand that specifies the source ref to fetch (master) but does not specify a target ref to update. Default behavior would apply, which with a typical setup means origin/master would be updated.

    The second example, you provide a source (master), then a : to separate that from a destination, then a destination (also master). This will attempt to directly update the local master branch of your local repo - rather than the origin/master remote branch ref. It may fail (e.g. if the local repo is a non-bare repo in a default configuration, and it has master checked out).

    If you want to use explicit refspec but you still want to update the remote ref, you could say

    git fetch origin master:origin/master