Search code examples
gitgit-pushgit-clonegit-checkoutgit-fetch

Branch name clashes when pushing


What I think I know

When I clone or fetch from a remote repository, the name of the remote is prefixed to the names of the branches imported from this remote.
For instance, if the remote is called origin in my repository, then after cloning the remote's master branch is called origin/master in my repository.

This behavior makes sure that there are never name clashes between imported branches to branches that already exist in my repository (whether they were created locally or imported from other repositories).

What I don't know (i.e. my question)

When I push to a remote repository, is my repository name prefixed (in the remote repository) to the names of the branches that get pushed?

If not, how are branch name clashes avoided?


Solution

  • What I think I know

    When I clone or fetch from a remote repository, the name of the remote is prefixed to the names of the branches imported from this remote.

    For instance, if the remote is called origin in my repository, then after cloning the remote's master branch is called origin/master in my repository.

    Its not exactly like this.
    When you clone a git repository git you get reference to all the branches but only master is checked out

    enter image description here

    Every time you checkout a branch git add a track branch to it

    enter image description here

    When I push to a remote repository, is my repository name prefixed (in the remote repository) to the names of the branches that get pushed?

    Whenever you type push git use an internal mechanism called refspec

    refspec

    The format of the refspec is an optional +, followed by <src>:<dst>, where <src> is the pattern for references on the remote side and <dst> is where those references will be written locally.
    The + tells Git to update the reference even if it isn’t a fast-forward.

    More reading:
    https://www.atlassian.com/git/tutorials/refs-and-the-reflog#refspecs