Track the remote master branch in git

 Track the remote master branch in git

I’m still learning my way around all the finer points of git. One thing that confused me for a while was why cloned repositories seemed to act differently than repositories I manually added the remote origin to. Specifically, on a cloned repo, git status would tell me how many commits I was ahead of the remote master branch, and I could do things like git pull instead of git pull origin master.

As I learned more, I realized the default master branch was set up as a remote tracking branch by default only if the repo was cloned. So on a repo that wasn’t cloned, I figured I’d set up master as a remote tracking branch just like any other:

git branch --track master origin/master

Of course, this fails because the master branch already exists. The solution was to manually edit .git/config and add this:

[branch "master"]
	remote = origin
	merge = refs/heads/master

Bingo. Now I could recklessly git pull and push without specifying the branches, and status lets me know when I’m ahead of origin.

Update: As suggested by Chris Saylor in the comments, you can add the above to your git config via the command line with:

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