git中的origin

origin

  • 首先假设你自己在github上创建了一个Repository,叫做myRepository,假设你的Github ID是user1,这个时候指向你的代码库的链接是
https://github.com/user1/myRepository
  • clone
    如果你在terminal里输入
git clone https://github.com/user1/myRepository

这个时候git就在本地拷贝一份托管在github上的代码库

  • 进入
cd myRepository
  • 查看
git remote -v

你会看到控制台输出

origin https://github.com/user1/myRepository.git (fetch)
origin https://github.com/user1/myRepository.git (push)

也就是说git为你默认创建了一个指向远端代码库的origin(因为你是从这个地址clone下来的)

总结来讲,顾名思义,origin就是一个名字,它是在你clone一个托管在Github上代码库时,git为你默认创建的指向这个远程代码库的标签, origin指向的是repository,master只是这个repository中默认创建的第一个branch。当你git push的时候因为origin和master都是默认创建的,所以可以这样省略,但是这个是bad practice,因为当你换一个branch再git push的时候,有时候就纠结了


假设你在远端开了一个分支叫dev,再将它clone到本地,本地修改后重新push,利用 git push origin dev,就可以push到对应的远端了。
git push origin dev操作的前提是你切换到了当前远程dev分支。

  • 查看远程分支
$ git branch -a

* dev
  master
  origin/dev
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

可以看到当前在dev分支下

  • 切换分支
$ git checkout master

Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

这样就切换去master分支了

猜你喜欢

转载自blog.csdn.net/niexia_/article/details/79422859