Tips: 本文创建于2016年5月12日,已超过 2 年,内容或图片可能已经失效!
  • git clone
  • git fetch #获取远程仓库的所有分支以及数据
  • git pull #其实是 git fetch 和 git merge 的一个组合操作
  • git push

git clone 支持的协议

ssh/git/http/https/ftp/ftps/rsync

git 远程操作

-> [email protected] ~/git ☺ git clone https://github.com/liuhonghe/test.git  
-> [email protected] ~/git ☺ cd test  
-> [email protected] ~/git/test git:(master) ☺ git fetch  
remote: Counting objects: 3, done.  
remote: Compressing objects: 100% (2/2), done.  
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0  
Unpacking objects: 100% (3/3), done.  
From https://github.com/liuhonghe/test  
   9210f41..b23af75  master     -> origin/master  
-> [email protected] ~/git/test git:(master) ☺ git log --oneline --decorate --graph --all  
* b23af75 (origin/master, origin/HEAD) Create test.txt  
* 9210f41 (HEAD -> master) Update README.md  
* eede676 Initial commit  
-> [email protected] ~/git/test git:(master) ☺ git merge origin/master  
Updating 9210f41..b23af75  
Fast-forward  
 test.txt | 1 +  
 1 file changed, 1 insertion(+)  
 create mode 100644 test.txt  
-> [email protected] ~/git/test git:(master) ✗ 1☺ git add test.txt  
-> [email protected] ~/git/test git:(master) ✗ ☺ git commit -m "add some local"  
[master 2d8a0bf] add some local  
 1 file changed, 1 insertion(+)  
# 假设远程上已经有人更新了文件  
-> [email protected] ~/git/test git:(master) ☺ git push  
Password for 'https://[email protected]':  
To https://[email protected]/liuhonghe/test.git  
 ! [rejected]        master -> master (fetch first)  
error: failed to push some refs to 'https://[email protected]/liuhonghe/test.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.  
-> [email protected] ~/git/test git:(master) 1☺ git pull  
remote: Counting objects: 3, done.  
remote: Compressing objects: 100% (2/2), done.  
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0  
Unpacking objects: 100% (3/3), done.  
From https://github.com/liuhonghe/test  
   b23af75..be08923  master     -> origin/master  
Auto-merging test.txt  
CONFLICT (content): Merge conflict in test.txt  
Automatic merge failed; fix conflicts and then commit the result.  
-> [email protected] ~/git/test git:(master) 1☺ vim test.txt #解决冲突  
-> [email protected] ~/git/test git:(master) 1☺ git add test.txt  
-> [email protected] ~/git/test git:(master) 1☺ git commit  # 本地的历史是最新的  
-> [email protected] ~/git/test git:(master) ☺ git push  
Password for 'https://[email protected]':  
Counting objects: 6, done.  
Delta compression using up to 4 threads.  
Compressing objects: 100% (5/5), done.  
Writing objects: 100% (6/6), 640 bytes | 0 bytes/s, done.  
Total 6 (delta 2), reused 0 (delta 0)  
To https://[email protected]/liuhonghe/test.git  
   be08923..7c62aea  master -> master  
-> [email protected] ~/git/test git:(master) ☺ git tag -a v0 -m "tag for v0"  
-> [email protected] ~/git/test git:(master) ☺ git push  
Password for 'https://[email protected]':  
Everything up-to-date  
-> [email protected] ~/git/test git:(master) ☺ git push --tag  
Password for 'https://[email protected]':  
Counting objects: 1, done.  
Writing objects: 100% (1/1), 168 bytes | 0 bytes/s, done.  
Total 1 (delta 0), reused 0 (delta 0)  
To https://[email protected]/liuhonghe/test.git  
 * [new tag]         v0 -> v0  
-> [email protected] ~/git/test git:(master) ☺ git pull  
From https://github.com/liuhonghe/test  
 * [new branch]      fetcher    -> origin/fetcher  
Already up-to-date.  
-> [email protected] ~/git/test git:(master) 1☺ git pull origin fetcher  
From https://github.com/liuhonghe/test  
 * branch            fetcher    -> FETCH_HEAD  
Already up-to-date.  
-> [email protected] ~/git/test git:(master) ☺ echo "add some test th" >> test.txt  
-> [email protected] ~/git/test git:(master) ✗ ☺ git add .  
-> [email protected] ~/git/test git:(master) ✗ ☺ git commit -m "add some test on master"  
[master ad07693] add some test on master  
 1 file changed, 1 insertion(+)  
-> [email protected] ~/git/test git:(master) ☺ git checkout v0  
Note: checking out 'v0'.  
  
You are in 'detached HEAD' state. You can look around, make experimental  
changes and commit them, and you can discard any commits you make in this  
state without impacting any branches by performing another checkout.  
  
If you want to create a new branch to retain commits you create, you may  
do so (now or later) by using -b with the checkout command again. Example:  
  
  git checkout -b <new-branch-name>  
  
HEAD is now at 7c62aea... Merge branch 'master' of https://github.com/liuhonghe/test  
-> [email protected] ~/git/test git:(7c62aea) ☺ git push origin master  
Password for 'https://[email protected]':  
Counting objects: 3, done.  
Delta compression using up to 4 threads.  
Compressing objects: 100% (3/3), done.  
Writing objects: 100% (3/3), 321 bytes | 0 bytes/s, done.  
Total 3 (delta 1), reused 0 (delta 0)  
To https://[email protected]/liuhonghe/test.git  
   7c62aea..ad07693  master -> master  
-> [email protected] ~/git/test git:(fetcher) ☺ git checkout master  
Switched to branch 'master'  
Your branch is up-to-date with 'origin/master'.  
-> [email protected] ~/git/test git:(master) ☺ git branch -d fetcher  
Deleted branch fetcher (was 7c62aea).  
-> [email protected] ~/git/test git:(master) ☺ git push --delete origin fetcher #删除远程分支  
Password for 'https://[email protected]':  
To https://[email protected]/liuhonghe/test.git  
 - [deleted]         fetcher  
-> [email protected] ~/git/test git:(master) ☺ git push origin :fetcher #第二种删除远程分支