Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。
github网站就是提供Git仓库托管服务的,所以,只要注册一个GitHub账号,就可以免费获得Git远程仓库。
github官网:https://github.com/
远程库
1. github版本库即为远程库。
本地库
1. 本地创建的Git版本库即为本地库。
使用说明
1. 先在github官网中注册账户。
2. 创建SSH key。
由于你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的。
Windows下打开Git Bash,创建SSH key。
$ ssh-keygen -t rsa -C "youremail@example.com"
3. 在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件(密钥对)。
用户主目录:C:\Users\Administrator
4. 登陆GitHub,打开"Account settings","SSH Keys"页面。
5. 点"Add SSH Key",填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容。
本地库 关联 远程库
origin:默认远程库名称
# 在本地库当前分支
$ git remote add origin git@github.com:pythonde/newRepository.git
上传数据到远程库
$ git push -u origin dev
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
To github.com:pythonde/newRepository.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:pythonde/newRepository.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
远程库更新本地库
$ git pull --rebase origin dev
From github.com:pythonde/newRepository
* branch dev -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: first add
Applying: dev second
Applying: test2 dev third
成功将本地库内容上传到远程库
$ git push -u origin dev
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 852 bytes | 852.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To github.com:pythonde/newRepository.git
a69bffd..da7dd11 dev -> dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
注意事项
1. 远程操作git push / git pull 时,本地库和远程库分支名相同。
2. 第一次push时:$ git push -u origin dev
3. 之后只需要简写:$ git push origin dev
4. 远程库分支之间的合并,创建一个pull request进行合并。
查看远程库信息
$ git remote -v
删除连接远程库
$ git remote rm origin
从远程库下载源码到本地库
$ git clone git@github.com:pythonde/newRepository.git