码迷,mamicode.com
首页 > 其他好文 > 详细

Git及Github

时间:2016-05-04 18:50:52      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

很久之前就想系统的学习一下版本控制系统了,这两天抽空看了一下《Progit》和《GotGithub》这两本书,学习了Git这个版本控制系统以及只支持Git作为唯一版本库格式的项目托管平台Github,这两者在项目开发中都是很好的工具,下面总结一下使用方法,以方便以后使用时查阅。

 

原理

下面的两张图片很好的解释了Git和Github的原理:

技术分享

分布式版本控制系统

 

技术分享

Github的协同模式

 

添加SSH认证

1. 生成SSH密钥

 

  1. $ ssh-keygen  
  2. Generating public/private rsa key pair.  
  3. Enter file in which to save the key (/Users/schacon/.ssh/id_rsa):  
  4. Enter passphrase (empty for no passphrase):  
  5. Enter same passphrase again:  
  6. Your identification has been saved in /Users/schacon/.ssh/id_rsa.  
  7. Your public key has been saved in /Users/schacon/.ssh/id_rsa.pub.  
  8. The key fingerprint is:  
  9. 43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local  

 

 

2. 添加公钥到Github

 

  1. $ cat ~/.ssh/id_rsa.pub  
  2. ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU  
  3. GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3  
  4. Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA  
  5. t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En  
  6. mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx  
  7. NrRFi9wrf+M7Q== schacon@agadorlaptop.local  

技术分享

 

3. SSH认证

 

  1. $ ssh -T git@github.com  
  2. Hi gotgithub! You‘ve successfully authenticated, but GitHub does not provide shell access.  

出现上面的提示则认证成功,以后就可以使用ssh协议(自动认证,不用输入口令)克隆远程仓库了。

 

 

 

配置用户信息

1. 配置全局信息

 

  1. $ git config --global user.name "John Doe"  
  2. $ git config --global user.email johndoe@example.com   

2. 配置本地仓库局部信息

首先进入当前仓库工作区

 

  1. $ git config user.name "John Doe"  
  2. $ git config user.email johndoe@example.com  

 

3. 查看当前配置信息

 

  1. $ git config --list  
  2. user.name=Scott Chacon  
  3. user.email=schacon@gmail.com  
  4. color.status=auto  
  5. color.branch=auto  
  6. color.interactive=auto  
  7. color.diff=auto  
  8. ...  
  9.   
  10. $ git config user.name  
  11. Scott Chacon  

 

 

 

 

本地仓库常用命令

1. 初始化

本地初始化:

  1. $ git init  
  2. $ git commit -m ‘initial project version  
  3. $ git remote add origin git@github.com:gotgithub/helloworld.git  
  4. $ git push -u origin master   

或者从远程仓库克隆:

  1. $ git clone git://github.com/schacon/grit.git  
  2. $ git clone git://github.com/schacon/grit.git mygrit  

 

 

2. 检查当前文件状态

  1. $ git status  
  2. # On branch master  
  3. nothing to commit (working directory clean)  

 

 

3. 添加文件到缓存区

  1. $ vim README  
  2. $ git status  
  3. # On branch master  
  4. # Untracked files:  
  5. #  
  6. (use "git add <file>..." to include in what will be committed)  
  7. #  
  8. # README  
  9. nothing added to commit but untracked files present (use "git add" to track)  
  10. $  
  11. $ git add README  
  12. $ git status  
  13. # On branch master  
  14. # Changes to be committed:  
  15. #  
  16. (use "git reset HEAD <file>..." to unstage)  
  17. #  
  18. # new file:   README  
  19. #  

 

 

4. 忽略某些文件

  1. $ cat .gitignore  
  2. *.[oa]  
  3. *~  

修改.gitignore文件即可。

 


5. 查看更新具体内容

查看工作目录中当前文件和缓存区域快照之间的差异:

  1. $ git diff  
  2. diff --git a/benchmarks.rb b/benchmarks.rb  
  3. index 3cb747f..da65585 100644  
  4. --- a/benchmarks.rb  
  5. +++ b/benchmarks.rb  
  6. @@ -36,6 +36,10 @@ def main  
  7. @commit.parents[0].parents[0].parents[0]  
  8. end  
  9. +  
  10. run_code(x, ‘commits 1‘) do  
  11. +  
  12. git.commits.size  
  13. +  
  14. end  
  15. +  
  16. run_code(x, ‘commits 2‘) do  
  17. log = git.commits(‘master‘, 15)  
  18. log.size  


查看缓存区文件和上次提交时的快照之间的差异:

  1. $ git diff --cached  
  2. diff --git a/README b/README  
  3. new file mode 100644  
  4. index 0000000..03902a1  
  5. --- /dev/null  
  6. +++ b/README2  
  7. @@ -0,0 +1,5 @@  
  8. +grit  
  9. + by Tom Preston-Werner, Chris Wanstrath  
  10. + http://github.com/mojombo/grit  
  11. +  
  12. +Grit is a Ruby library for extracting information from a Git repository  



6. 提交更新到本地仓库

从缓存区提交:

  1. $ git commit -m "Story 182: Fix benchmarks for speed"  
  2. [master]: created 463dc4f: "Fix benchmarks for speed"  
  3. 2 files changed, 3 insertion  



从工作区提交:

  1. $ git commit -a -m ‘added new benchmarks‘  
  2. [master 83e38c7] added new benchmarks  
  3. 1 files changed, 5 insertions(+), 0 deletions(-)  



7. 移除文件

  1. $ rm grit.gemspec  
  2. $ git status  
  3. # On branch master  
  4. #  
  5. # Changed but not updated:  
  6. #  
  7. (use "git add/rm <file>..." to update what will be committed)  
  8. #  
  9. #  
  10. deleted:   grit.gemspec  
  11. #  
  12.   
  13. $ git rm grit.gemspec  
  14. rm ‘grit.gemspec‘  
  15. $ git status  
  16. # On branch master  
  17. #  
  18. # Changes to be committed:  
  19. #  
  20. (use "git reset HEAD <file>..." to unstage)  
  21. #  
  22. #  
  23. deleted:   grit.gemspec  
  24. #  


直接从缓存区移除文件:

  1. $ git rm --cached readme.txt  

 

 

8. 移动文件

  1. $ git mv src/main.cpp ./main.cpp  
  2. $ git status  
  3. # On branch master  
  4. # Your branch is ahead of ‘origin/master‘ by 3 commits.  
  5. #  
  6. # Changes to be committed:  
  7. #   (use "git reset HEAD <file>..." to unstage)  
  8. #  
  9. #    renamed:    src/main.cpp -> main.cpp  
  10. #  



9. 查看提交历史

  1. $ git log  
  2. commit ca82a6dff817ec66f44342007202690a93763949  
  3. Author: Scott Chacon <schacon@gee-mail.com>  
  4. Date:  
  5. Mon Mar 17 21:52:11 2008 -0700  
  6. changed the verison number  
  7. commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7  
  8. Author: Scott Chacon <schacon@gee-mail.com>  
  9. Date:  
  10. Sat Mar 15 16:40:33 2008 -0700  
  11. removed unnecessary test code  
  12. commit a11bef06a3f659402fe7563abf99ad00de2209e6  
  13. Author: Scott Chacon <schacon@gee-mail.com>  
  14. Date:  
  15. Sat Mar 15 10:31:28 2008 -0700  
  16. first commit  

如果使用Github托管项目的话到Github上去看比较清楚。

 

10. 覆盖上一次提交

  1. $ git commit -m ‘initial commit‘  
  2. $ git add forgotten_file  
  3. $ git commit --amend  

新增的文件并入到上次提交的内容一起提交,覆盖上次提交。

 

11. 取消缓存区中的文件到工作区

  1. $ git reset HEAD benchmarks.rb  
  2. benchmarks.rb: locally modified  
  3. $ git status  
  4. # On branch master  
  5. # Changes to be committed:  
  6. #  
  7. (use "git reset HEAD <file>..." to unstage)  
  8. #  
  9. #  
  10. modified:   README.txt  
  11. #  
  12. # Changed but not updated:  
  13. # (use "git add <file>..." to update what will be committed)  
  14. # (use "git checkout -- <file>..." to discard changes in working directory)  
  15. #  
  16. #  
  17. #  
  18. modified:   benchmarks.rb  

 

 

12. 新建以及切换分支

 

  1. $ git branch testing        #新建分支  
  2.   
  3. $ git checkout testing      #切换分支  
  4.   
  5. $ git checkout -b testing   #新建并切换分支  

 

 

13. 合并分支

 

  1. $ git checkout master  
  2. $ git merge hotfix  
  3. Updating f42c576..3a0874c  
  4. Fast forward  
  5. README |  
  6. 1 -  
  7. 1 files changed, 0 insertions(+), 1 deletions(-)  

 

 

14. 删除分支

 

  1. $ git branch -d hotfix  
  2. Deleted branch hotfix (3a0874c).  
  3.   
  4. #强制删除未合并的分支  
  5.   
  6. $ git branch -D mybranch1  
  7. Deleted branch mybranch1 (was f46a284).   



15. 分支管理

  1. $ git branch iss53   
  2. * master   
  3. testing   
  4.   
  5. #查看哪些分支已被并入当前分支   
  6. $ git branch --merged   
  7. iss53   
  8. * master   
  9.   
  10. #查看尚未合并的分支   
  11. $ git branch --no-merged testing   



 

与远程仓库交互

1. 克隆仓库到本地

 

  1. $ git clone git://github.com/schacon/ticgit.git  
  2. Initialized empty Git repository in /private/tmp/ticgit/.git/  
  3. remote: Counting objects: 595, done.  
  4. remote: Compressing objects: 100% (269/269), done.  
  5. remote: Total 595 (delta 255), reused 589 (delta 253)  
  6. Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.  
  7. Resolving deltas: 100% (255/255), done.  

 

 

2. 查看当前远程库

 

  1. $ git remote  
  2. origin  
  3. $ git remote -v  
  4. origin git://github.com/schacon/ticgit.git  
  5. $ git remote -v  
  6. bakkdoor git://github.com/bakkdoor/grit.git  
  7. cho45 git://github.com/cho45/grit.git  
  8. defunkt git://github.com/defunkt/grit.git  
  9. koke git://github.com/koke/grit.git  
  10. origin git@github.com:mojombo/grit.git  



 

3. 添加远程仓库

 

  1. $ git remote  
  2. origin  
  3. $ git remote add pb git://github.com/paulboone/ticgit.git  
  4. $ git remote -v  
  5. origin git://github.com/schacon/ticgit.git  
  6. pb git://github.com/paulboone/ticgit.git  



 

4. 从远程仓库抓取数据

 

  1. $ git fetch [remote-name]  



 

5. 推送数据到远程仓库

 

  1. $ git push [remote-name] [branch-name]  
  2.   
  3. $ git push origin master  
  4.   
  5. $ git push [远程名] [本地分支]:[远程分支]  
  6.   
  7. $ git push [远程名] :[远程分支]       #删除远程分支  



 

6. 查看远程仓库信息

 

  1. $ git remote show [remote-name]  
  2.   
  3. $ git remote show origin  
  4. * remote origin  
  5. URL: git://github.com/schacon/ticgit.git  
  6. Remote branch merged with ‘git pull‘ while on branch master  
  7. master  
  8. Tracked remote branches  
  9. master  
  10. ticgit  



 

7. 远程仓库的删除和重命名

 

  1. $ git remote rename pb paul  
  2. $ git remote  
  3. origin  
  4. paul  
  5.   
  6. $ git remote rm paul  
  7. $ git remote  
  8. origin  

 

 

 

8. 在远程仓库中创建分支

将mybranch1分支推送到远程仓库中,在远程仓库中新建分支,并添加追踪

 

  1. $ git push -u origin mybranch1  
  2. Counting objects: 4, done.  
  3. Delta compression using up to 2 threads.  
  4. Compressing objects: 100% (2/2), done.  
  5. Writing objects: 100% (3/3), 281 bytes, done.  
  6. Total 3 (delta 0), reused 0 (delta 0)  
  7. To git@github.com:gotgithub/helloworld.git  
  8.  * [new branch]      mybranch1 -> mybranch1  
  9. Branch mybranch1 set up to track remote branch mybranch1 from origin.  



 

 

Tag管理

1. tag的创建

 

  1. $ git tag <tagname> [<commit>]  
  2.   
  3. $ git tag -m "Tag on initial commit" mytag1 HEAD^  
  4. $ git tag -m "Tag on new commit"     mytag2  

 

2. 查看tag

 

  1. $ git tag -l -n1  
  2. mytag1          Tag on initial commit  
  3. mytag2          Tag on new commit  

 

 


3. 将本地tag推送到远程仓库中

 

  1. $ git push origin refs/tags/*  
  2. Counting objects: 6, done.  
  3. Delta compression using up to 2 threads.  
  4. Compressing objects: 100% (4/4), done.  
  5. Writing objects: 100% (5/5), 548 bytes, done.  
  6. Total 5 (delta 0), reused 0 (delta 0)  
  7. To git@github.com:gotgithub/helloworld.git  
  8.  * [new tag]         mytag1 -> mytag1  
  9.  * [new tag]         mytag2 -> mytag2  

 

 

4. 删除本地tag

 

  1. $ git tag -d mytag3  
  2. Deleted tag ‘mytag3‘ (was c71231c)  

 

 

5. 删除远程仓库中的tag

 

    1. $ git push origin :mytag3  
    2. To git@github.com:gotgithub/helloworld.git  
    3.   [deleted]         mytag3  

Git及Github

标签:

原文地址:http://www.cnblogs.com/dahuangwangwang/p/5459352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!