简体   繁体   English

Git如何知道要推送到哪个存储库?

[英]How does Git know which repository to push to?

I'm a complete noob when it comes to version control, but I recently started using GitHub to host some of my projects. 在版本控制方面,我是一个完整的菜鸟,但我最近开始使用GitHub来托管我的一些项目。 I blindly use the command git push origin master to push changes to either of the two repositories. 我盲目地使用命令git push origin master将更改推送到两个存储库中的任何一个。 I don't understand how Git knows which repository to push to. 我不明白Git如何知道要推送到哪个存储库。 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. 谢谢你为我清理这个。

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有一个远程系统,允许指定URL和传输到除你工作的存储库之外的存储库。 git push origin master pushes the current branch to the remote called origin as the branch master. git push origin master将当前分支作为分支主机推送到远程调用源。 You have a remote called origin. 你有一个远程叫做起源。 This is created by default when you clone a repository from a URL. 从URL克隆存储库时,默认情况下会创建此选项。

git remote add origin http://abc.com/def/ghi.git告诉git远程'origin'指向的url。

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. 当您使用git push origin master ,您没有推送到两个存储库 - 您正在推送到存储库中的主分支,其名称(别名)是源。

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. 如果你是新手并通过git init设置git,你可能不会推送到两个存储库。 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): 这将返回类似的东西(假设您有像github这样的git托管服务):

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. git@github.com:yourGithubName/fileName.git是您的存储库地址。 Some addresses are prefixed by https:// . 某些地址以h​​ttps://为前缀。

Git repositories contain a .git directory that contains metadata about the repository. Git存储库包含一个.git目录,其中包含有关存储库的元数据。 That's what Git uses to determine where to push your changes. 这就是Git用来确定推送更改的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM