Ben Pearce
Ben Pearce

Reputation: 7094

What does git config do?

I'm new to git and I'm working through a tutorial on setting up subModules. I'm trying to understand all the commands and getting hung up on the lines below.

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

Can someone explain what git config does and how to interpret

branch.master.remote
branch.master.merge

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial

Upvotes: 2

Views: 4482

Answers (2)

VonC
VonC

Reputation: 1324278

By default, git config will modify the local config for your current git repo.
(There are three levels of config: system, global and local)

The git config branch.master.xxx you mention is for setting up an upstream branch to your local branch master, in order for your local repo to know what and where to push to from the master branch.

That tutorial is quite old, and nowadays (1.8.0+), we would use:

git branch -u origin/master master

That would modify the same config file, but you wouldn't have to be aware of the branch config syntax.

And you would be able to review all the upstream branches with git branch:

git branch -avv

Specifying an upstream branch is mandatory for the first push, as explained in "Why do I need to explicitly push a new branch?".

Upvotes: 0

elmart
elmart

Reputation: 2374

To be able to merge changes from an upstream branch into one of your local branches, you need two pieces of info: - The remote where the upstream branch lives. - The name of the upstream branch.

With that, and once you have your target local branch checked out, you can issue the following command:

git pull <upstream_remote> <upstream_branch>

to do the intended merge.

The most common case, merging master branch of remote origin into your local master branch, would then be:

git pull origin master

But it would be tiresome having to type that again and again. So, you can save some typing saying to git to remember those two pieces of info (remote and branch), so that you can type just git pull.

That's what you git config commands are doing. Remembering, for your local branch master, the two needed pieces of info:

git config branch.master.remote origin

"When pulling into master, use remote origin"

git config branch.master.merge refs/heads/master

"When pulling into master, use branch master of remote"

As you see, concepts are easy. It's just the syntax that's a bit convoluted. Two final notes on that:

  • refs/heads/master is the name of the remote branch as seen by the remote repo.
  • refs/heads/master is canonical form for what is normally referred as just master. We use canonical names in certain places of config.

Lastly, as @VonC sai, we would nowadays do that through more polished/simple commands, but I think it's worth to know what git is doing under the covers, and the git config commands nicely expose it in this case.

Upvotes: 2

Related Questions