Alex Rivera | Logout

What is "git remote add ..." and "git push origin master"?

Asked 2011-04-11T05:43:44.833
325

Quite often, Git and Ruby on Rails looks like magic... such as in the first chapter of Ruby on Rails 3 Tutorial book, it talks about Git:

git remote add origin git@github.com:peter/first_app.git
git push origin master

And it pretty much says "it just works" without saying too much about what they are and start talking about branching. Searching on the Internet shows that git remote add is to add a "short name", such as origin, and it can be any name as well, which is like an alias to a URL.

And origin is the usual path of where the remote repository points to (in http://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes under "Adding Remote Repositories").

So why is the URL not git://git@github.com/peter/first_app.git, but in the other syntax -- what syntax is it? Why must it end with .git? I tried not using .git at the end and it works too. If not .git, what else can it be? The git in git@github.com seems to be a user account on the Git server?

Also, why does it need to be so verbose to use git push origin master? Can't the default be origin and master? I found that the first time, the origin master is needed, but after a small edit and commit, then git push is all it needs (no need origin master). Can somebody who knows what is going on give some details?

Sometimes it feels like a lot of magic without explanation... and sometimes the person using it is so confident and when asked why, can't explain it, an

Edit
Report

1 Answer

7
  1. The .git at the end of the repository name is just a convention. Typically, on Git servers, repositories are kept in directories named project.git. The Git client and protocol honours this convention by testing for project.git when only project is specified.

  2. git://git@github.com/peter/first_app.git is not a valid Git URL. Git repositories can be identified and accessed via various URL schemes specified here. git@github.com:peter/first_app.git is the ssh URL mentioned on that page.

  3. Git is flexible. It allows you to track your local branch against almost any branch of any repository. While master (your local default branch) tracking origin/master (the remote default branch) is a popular situation, it is not universal. Many a times you may not want to do that. This is why the first git push is so verbose. It tells Git what to do with the local master branch when you do a git pull or a git push.

  4. The default for git push and git pull is to work with the current branch's remote. This is a better default than origin master. The way git push determines this is explained here.

git is fairly elegant and comprehensible, but there is a learning curve to walk through.

answered 2011-04-11T06:16:51.267

Your Answer