5

I'm a complete noob when it comes to version control, but I recently started using GitHub to host some of my projects. I blindly use the command git push origin master to push changes to either of the two repositories. I don't understand how Git knows which repository to push to. I use the same command to push to each. Does the directory I'm in have anything to do with it?

Thanks for clearing this up for me.

ICoffeeConsumer
  • 882
  • 1
  • 8
  • 22
  • Check the [link here](http://stackoverflow.com/questions/12195747/pushing-a-repository-onto-github/12195854#12195854), – thar45 Sep 26 '12 at 04:27
  • Why you are 'blindly use' some command, without knowing what it actually mean and what it does. To avoid mess up after certain stage it will be better if you spend some time with git documentation and work flow. – suvankar Sep 26 '12 at 04:37

4 Answers4

3

A word of advice, "blindly use"ing anything is a bad idea.

git has a system of remotes which allows to specify URLs and transports to repositories other than the one where you're working. git push origin master pushes the current branch to the remote called origin as the branch master. You have a remote called origin. This is created by default when you clone a repository from a URL.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • I'm confused because I can literally push using an identical command in any of my repos (since I named both remotes 'origin'). It's not like origin is some sort of global Git variable or something, so it must be a directory-specific thing. I think the .git directory that Fraxtill mentioned is probably responsible for this. – ICoffeeConsumer Sep 26 '12 at 04:39
  • 2
    The `git` directory stores all the information of your project. It is *the* repository. Delete it and the directory is no longer a repository. The actual list of remotes and the URLs they point to are in `.git/config`. – Noufal Ibrahim Sep 26 '12 at 05:31
3

git remote add origin http://abc.com/def/ghi.git tells git the url that remote 'origin' points to.

linquize
  • 19,828
  • 10
  • 59
  • 83
3

When you use git push origin master, you aren't pushing to two repositories - you are pushing to the master branch in your repository who's name (alias) is origin.

Think of it like this:

I'm pushing my stuff to origin which is my repository address. What am I pushing there? My master branch.

If you are new and set git up yourself through git init, you probably aren't pushing to two repositories. Run this command to see what your remotes are:

git remote -v

This will return something like (assuming you have a git hosting service like github):

blahblah    git@github.com:yourGithubName/fileName.git (fetch)
blahblah    git@github.com:yourGithubName/fileName.git (push)

git@github.com:yourGithubName/fileName.git is your repository address. Some addresses are prefixed by https:// .

gitlinggun
  • 108
  • 6
1

Git repositories contain a .git directory that contains metadata about the repository. That's what Git uses to determine where to push your changes.

ashastral
  • 2,818
  • 1
  • 21
  • 32
  • 1
    Does this mean we have to `cd` to the correct directory first? What if we are in subfolders of that repo? What happens if we are in a wrong directory without a .git direcotry? – Miladiouss Jan 22 '18 at 20:06