Here's the contents of the remote
and branch
sections of my .git/config
file.
[remote "origin"] url = https://[email protected]/EvanAad/bitbucketstationlocations.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master
What is the meaning and purpose of the contents of these sections, in particular the fetch
and merge
subsections? How is this information used by Git to guide its operation?
Its called refspec. Its the mechmism that git is using to "talk" to the remote server and to map local branches to remote branches.
A refspec maps a branch in the local repository to a branch in a remote repository.
This makes it possible to manage remote branches using local Git commands and to configure some advanced git push and git fetch behavior.
A refspec is specified as [+]<src>:<dst>
. The <src>
parameter is the source branch in the local repository, and the <dst>
parameter is the destination branch in the remote repository.
The optional +
sign is for forcing the remote repository to perform a non-fast-forward update.
Refspecs can be used with the git push command to give a different name to the remote branch. For example, the following command pushes the master branch to the origin remote repo like an ordinary git push, but it uses qa-master as the name for the branch in the origin repo. This is useful for QA teams that need to push their own branches to a remote repo.
git push origin master:refs/heads/qa-master
By adding a few lines to the Git configuration file, you can use refspecs to alter the behavior of git fetch.
By default, git fetch
fetches all of the branches in the remote repository. The reason for this is the following section of the .git/config
file:
[remote "origin"]
url = https://[email protected]:mary/example-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
The fetch
line tells git fetch to download all of the branches from the origin repo.
But, some workflows don’t need all of them. For example, many continuous integration workflows only care about the master branch. To fetch only the master branch, change the fetch line to match the following:
[remote "origin"]
url = https://[email protected]:mary/example-repo.git
fetch = +refs/heads/master:refs/remotes/origin/master
You can also configure git push in a similar manner. For instance, if you want to always push the master branch to qa-master in the origin remote (as we did above), you would change the config file to:
[remote "origin"]
url = https://[email protected]:mary/example-repo.git
fetch = +refs/heads/master:refs/remotes/origin/master
push = refs/heads/master:refs/heads/qa-master
Refspecs give you complete control over how various Git commands transfer branches between repositories.
They let you rename and delete branches from your local repository, fetch/push
to branches with different names, and configure git push and git fetch to work with only the branches that you want.