3

I was trying to deploy my source code onto Github. Then, I first added the github source to remote source as follows:

$ git remote add origin [email protected]:user_name/foo_bar.git*

Then, I tried to run "git push" command to upload my source code onto my remote github account as below.

$ git push

Enter passphrase for key '/c/Users/mylaptop/.ssh/id_rsa':
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '[email protected]:user_name/foo_bar.git'

As I learned from railstutorial.org, I know if I use "git push" for the first time, it will return an error expecting me to specify 'origin master' parameters. But for the next and later times, I can only fire up "git push", it will work gracefully for me. What is the reason for that? as I thought the command by default always refers to the branch 'master'.

Any clear explanation would be really appreciated.

3 Answers 3

6

The first time you do git push, there is no remote branch called master on origin.

After the branch exists, you can use git push and it will link to the remote branch with the same name of the branch you are currently on.

Sign up to request clarification or add additional context in comments.

Comments

2

The default behavior of git push origin is to sent to origin all local branches that have the same name as a branch on origin.

If you type git push without specifying origin, the name of the destination repository is inferred from the remote branch tracked by the branch you are on. So if you are on a branch master tracking origin/master, git push is similar to git push origin.

Comments

1

Git repositories have this concept of "tracking". So, from git's perspective it can know that the master branch is tracking origin/master. If it sees that relationship when you do a git push then it says "Oh, well he's pushing from master, which tracks with origin/master. That must be where it wants it to go."

However, before your first push - git doesn't know about this relationship which means your command is (potentially) ambiguous. You can read the official documentation about this behavior in the Git Book's Chapter on Tracking.

Note: See the comment by Jefromi below. He points out some inaccuracies in my explanation that could be important, depending on your use case.

2 Comments

This isn't really correct, and that passage in the Git book is a bit misleading too. (I think it's trying to simply, not give out false information, but still.) By default, git-push pushes to branches of the same name, completely ignoring tracking branches. If you want it to use tracking branches, you have to set push.default to tracking. In normal cases, though, everything will Just Work - all branches track branches of the same name, and you avoid accidentally pushing your personal local branches because they don't exist on the remote.
Gah, you're correct. I've been using git for about 9 months now, successfully set up two private git servers - and I still have stuff to learn. Such is life. Thanks for the comment! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.