(背景:之前有限度接触过svn和cvs,曾经架设过一个svn服务器但从未投入实际使用。)
首先阅读了GitHub的4篇教程,分别是Set Up Git、Create a Repository、Fork a Repository、Be Social。实践了前两篇。
教程中关键的命令如下:
git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named “origin” pointing at your GitHub repository
git push origin master
# Sends your commits in the “master” branch to GitHub
练习时首先在办公室将一个python文件add/commit/push到GitHub上,回到家里后在家里的计算机上做了些更改再commit/push到GitHub。第二天回到办公室修改代码,试图push时提示:
$ git push origin master
To https://github.com/****/Hello-World.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/****/Hello-World.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 merge the remote changes (e.g.,
hint: ‘git pull’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
于是按照提示运行
$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/****/Hello-World
8e27c93..9d246e3 master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull
If you wish to set tracking information for this branch you can do so with:
git branch –set-upstream-to=origin/ master
由此可知不管push还是pull,都要指定remote和branch。这里remote是origin, branch是master。
再运行
$ git pull origin master
$ git push origin master
本地代码和GitHub就同步了。
PS:发现了一个有用的工具cheat。可以通过gem install cheat来安装,然后运行cheat git快速参考git的用法。另外尝试了cheat cheat 和cheat vim。初次使用时下载cheatsheet有点慢。