参考:GitHub使用前的准备工作这篇文章,在GitHub上创建一个仓库,设置为本地仓库的远程仓库。创建时建议不要勾选Initialize this repository with a README,这样会与本地仓库失去整合性。

推送

git remote add——添加远程仓库

GitHub上创建的仓库路径为“git@github.com:用户名 /git-tutorial.git”。现在我们用git remote add命令将它设置成本地仓库的远程仓库:

$ git remote add peek git@github.com:eyujungit/paopaojun.git
按照上述格式执行git remote add命令之后,Git会自动将git@github.com:eyujungit/paopaojun.git远程仓库的名称设置为peek(标识符)。 git push——推送至远程仓库 将当前分支下本地仓库中的内容推送给远程仓库,使用git push命令。在master分支下执行操作:
$ git branch
* master

$ git push -u peek master

Enter passphrase for key '/c/Users/xjj eyujun/.ssh/id_rsa':  输入账户密码
Counting objects: 22, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (22/22), 1.77 KiB | 0 bytes/s, done.
Total 22 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To git@github.com:eyujungit/paopaojun.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from peek.

执行上面的命令,当前分支的内容就会被推送给远程仓库peek的master分支。-u将peek仓库的master分支设置为本地仓库当分支的上游,在使用git pull命令获取内容时,本地仓库的这个分支就可以直接从peek的master分支获取内容。推送成功,登录GitHub查看一下,master分支已经被更新!

现在在本地仓库创建four分支,并将它推送至远程仓库:
$ git checkout -b four
Switched to a new branch 'four'

$ vi test_four.py  //这里我添加了一个文件

$ git add test_four.py

$ git commit -m 'add four'
[four c2a0e86] add four
 1 file changed, 1 insertion(+)
 create mode 100644 test_four.py


$ git push -u peek four  //push给远程仓库
Enter passphrase for key '/c/Users/xjj eyujun/.ssh/id_rsa':
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 317 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'four' on GitHub by visiting:
remote:      https://github.com/eyujungit/paopaojun/pull/new/four
remote:
To git@github.com:eyujungit/paopaojun.git
 * [new branch]      four -> four
Branch four set up to track remote branch four from peek.
现在在GitHub远程仓库,你会看到four分支的内容了!

获取

新建一个本地仓库,用于获取刚刚推送的four分支。下面的操作,不要与之前的仓库在同一目录下。 git clone——获取远程仓库
$ git clone git@github.com:eyujungit/paopaojun.git

Cloning into 'paopaojun'...
Enter passphrase for key '/c/Users/xjj eyujun/.ssh/id_rsa': 密码
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 25 (delta 3), reused 24 (delta 2), pack-reused 0
Receiving objects: 100% (25/25), done.
Resolving deltas: 100% (3/3), done.
Checking connectivity... done.


$ git branch -a   //-a同时显示本地仓库和远程仓库的分支信息
* four
  master
  one
  three
  two
  remotes/peek/four
  remotes/peek/master
现在获取远程仓库中的four到本地:
$ git checkout -b four peek/four
// -b参数后面的名字是新建分支的名称four
// four后面的是获取原来分支的名称,名为peek仓库的four分支
修改README.md文件,并向本地的four分支提交修改:
$ git commit -am 'Add Four'
$ git push 
从远程仓库获取了four分支,在本地仓库中提交了更改,再将four分支推回远程仓库。 git pull——获取最新的远程仓库

放下刚刚操作的呢个目录,回到原先的呢个目录下。现在使用git pull命令。将本地的four分支更新到最新状态,切换到four分支下:

$ git checkout four

$ git pull peek four
Enter passphrase for key '/c/Users/xjj eyujun/.ssh/id_rsa': 密码
From github.com:eyujungit/paopaojun
 * branch            four       -> FETCH_HEAD
Already up-to-date.
我这里已经显示是最新的了,不知道为啥!

发表评论

后才能评论