如何使用git

来源:互联网 发布:php个人简历源代码 编辑:程序博客网 时间:2024/04/30 16:54

     一、git的简介

Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。 Git 是 Linus             Torvalds 为了帮助管理 Linux 内核          开发而开发的一个开放源码的版本控制软件。
Git跟踪并管理的是修改,而非文件。
二、git的安装(linux)
命令安装: 
yum install git git-svn git-email git-gui gitk
安装完成后,还需要最后一步设置,在命令行输入:
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。

三、创建版本库

    (1)创建目录
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www admin]# mkdir KEY  
  2. [root@www admin]# cd KEY  
  3. [root@www KEY]# pwd  
  4. /home/admin/KEY  

    (2)通过git init命令把这个目录变成Git可以管理的仓库:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git init  
  2. Initialized empty Git repository in /home/admin/KEY/.git  

当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# ls -a  
  2.      .  ..  .git  

四、把文件添加到仓库中

工作区 
相当于创建的目录

版本库(Repository) 

工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。版本库里面包含一个暂存区,创建的第一个分支master,指向master的指针head。

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# vim test.c   //修改test.c  
  2.      [root@www KEY]# git add test.c  
  3.      [root@www KEY]# git commit -m "add a file"  
  4.      [master (root-commit) 2ff3615] add a file  
  5.      1 files changed, 6 insertions(+), 0 deletions(-)  
  6.      create mode 100644 test.c  
  7.      [root@www KEY]# cat test.c  
  8.      #include<stdio.h>  
  9.      int main()  
  10.     {  
  11.     printf("hello world\n");  
  12.     return 0;  
  13.     }  
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# vim test.c  
  2. [root@www KEY]# git add test.c  
  3. [root@www KEY]# git status  
  4. # On branch master  
  5. # Changes to be committed:  
  6. #   (use "git reset HEAD <file>..." to unstage)  
  7. #  
  8. #   modified:   test.c  
  9. #  
  10. [root@www KEY]# vim LICENSE  
  11. [root@www KEY]# git add LICENSE   
  12. [root@www KEY]# git status  
  13. # On branch master  
  14. # Changes to be committed:  
  15. #   (use "git reset HEAD <file>..." to unstage)  
  16. #  
  17. #   new file:   LICENSE  
  18. #   modified:   test.c  
  19. #  
  20. [root@www KEY]# git commit -m "modify 2 file"  
  21. [master 7050e08] modify 2 file  
  22.  2 files changed, 2 insertions(+), 1 deletions(-)  
  23.  create mode 100644 LICENSE  
  24. [root@www KEY]# git status  
  25. # On branch master  
  26. nothing to commit (working directory clean)  

git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

五、版本回退

(1)用git log查看修改内容
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git log  
  2. commit 87dfe8d2260bce1ad3c95d2bcab79662792346b3  
  3. Author: youngyoungla <634713275@qq.com>  
  4. Date:   Sat Jun 11 00:37:15 2016 -0700  
  5.   
  6.     modify  
  7.   
  8. commit 7050e08c5656a53f6f85389be61a50b1d999fd63  
  9. Author: youngyoungla <634713275@qq.com>  
  10. Date:   Sat Jun 11 00:31:07 2016 -0700  
  11.   
  12.     modify 2 file  
  13.   
  14. commit 2ff3615299e183d970e94315fa2467bd605c51eb  
  15. Author: youngyoungla <634713275@qq.com>  
  16. Date:   Sat Jun 11 00:19:00 2016 -0700  
  17.   
  18.     add a file  
     如果嫌麻烦,可以加上--pretty=oneline
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git log --pretty=oneline  
  2. 87dfe8d2260bce1ad3c95d2bcab79662792346b3 modify  
  3. 7050e08c5656a53f6f85389be61a50b1d999fd63 modify 2 file  
  4. 2ff3615299e183d970e94315fa2467bd605c51eb add a file  
      前面那一串是commit id,每提交一个新版本,实际上Git就会把它们自动串成一条时间线.
(2)版本回退
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提 交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就 是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
回退到上个版本:
1. git reset --hard HEAD^命令
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# cat test.c  
  2. #include<stdio.h>  
  3. int main()  
  4. {  
  5.     printf("we are young\n");  
  6.     printf("I Love You\n");  
  7.     return 0;  
  8. }  
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git reset --hard HEAD^  
  2. HEAD is now at 7050e08 modify 2 file  
  3. [root@www KEY]# cat test.c  
  4. #include<stdio.h>  
  5. int main()  
  6. {  
  7. <span style="white-space:pre">    </span>printf("we are young\n");  
  8. <span style="white-space:pre">    </span>return 0;  
  9. }  
2.使用commit id回退
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git reset --hard 87dfe8d2260bce1ad3c95d2bcab79662792346b3   
  2. HEAD is now at 87dfe8d modify  
  3. [root@www KEY]# cat test.c  
  4. #include<stdio.h>  
  5. int main()  
  6. {  
  7.     printf("we are young\n");  
  8.     printf("I Love You\n");  
  9.     return 0;  
  10. }  
(3)Git提供了一个命令git reflog用来记录你的每一次命令:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git reflog  
  2. 87dfe8d HEAD@{0}: 87dfe8d2260bce1ad3c95d2bcab79662792346b3: updating HEAD  
  3. 7050e08 HEAD@{1}: HEAD^: updating HEAD  
  4. 87dfe8d HEAD@{2}: commit: modify  
  5. 7050e08 HEAD@{3}: commit: modify 2 file  
  6. 2ff3615 HEAD@{4}: commit (initial): add a file</span>  
  7.   
  8. </span>  

六、撤销修改

(1)命令git checkout -- test.c意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。       总之,就是让这个文件回到最近一次git commit或git add时的状态。
      (2)git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。

七、删除文件

(1)在工作区上删除文件 rm test.c
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;color:#666666;">[root@www KEY]# rm LICENSE   
  2. rm:是否删除普通文件 "LICENSE"?y  
  3. [root@www KEY]# ls  
  4. test.c</span>  
        这时只是工作区上删除了文件,版本库上并没有删除,如果删错了,可以git checkout -- test.c还原。
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git checkout -- LICENSE  
  2. [root@www KEY]# ls  
  3. LICENSE  test.c  
  4. [root@www KEY]# cat LICENSE   
  5. lllalala  
     (2)从版本库上删除
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git rm LICENSE   
  2. rm 'LICENSE'  
  3. [root@www KEY]# ls  
  4. test.c  
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git checkout -- LICENSE  
  2. error: pathspec 'LICENSE' did not match any file(s) known to git.  
  3. [root@www KEY]# ls</span>  
  4. test.c  
       这时版本库上的文件已经被删除了,不能进行使用git checkout -- LICENSE还原,可以使用git reset --hard LICENSE回退版本。git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git reset --hard HEAD^  
  2. HEAD is now at 7050e08 modify 2 file  
  3. [root@www KEY]# ls  
  4. LICENSE  test.c  

八、远程仓库

(1)创建SSH KEY
ssh-keygen -t rsa -C "youremail@example.com"

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsaid_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:

然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:


点“Add Key”,你就应该看到已经添加的Key:



(2)添加远程库
首先,登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库KEY,
在本地的KEY仓库下运行命令:
[root@www KEY]# git remote add origin git@github.com:youngyoungla/KEY.git
将本地库传送到远程库上:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git push -u origin master  
  2. Counting objects: 7, done.  
  3. Compressing objects: 100% (5/5), done.  
  4. Writing objects: 100% (7/7), 599 bytes, done.  
  5. Total 7 (delta 0), reused 0 (delta 0)  
  6. To git@github.com:youngyoungla/KEY.git  
  7.  * [new branch]      master -> master  
  8. Branch master set up to track remote branch master from origin.  

把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样;

(3)远程库克隆
首先,登陆GitHub,创建一个新的仓库,名字叫gitskills1

我们勾选Initialize this repository with a README,这样GitHub会自动为我们创建一个README.md文件。创建完毕后,可以看到README.md文件;

远程库克隆:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git clone git@github.com:youngyoungla/gitskills1.git  
  2. Initialized empty Git repository in /home/admin/KEY/gitskills1/.git/  
  3. Warning: Permanently added the RSA host key for IP address '192.30.252.121' to the list of known hosts.  
  4. remote: Counting objects: 3, done.  
  5. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0  
  6. Receiving objects: 100% (3/3), done.  
  7. [root@www KEY]# ls  
  8. gitskills1  LICENSE  test.c  


九、分支管理

(1)创建分支 git checkout -b dev

每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。



[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git checkout -b dev //创建分支,head向前移动  
  2. Switched to a new branch 'dev'  
  3. [root@www KEY]# git branch   //查看分支  
  4. * dev  
  5.   master  
  6. [root@www KEY]# vim test.c     //修改test.c  
  7. [root@www KEY]# git add test.c     //添加并提交到仓库  
  8. [root@www KEY]# git commit -m "modify"  
  9. [dev 86a10c8] modify  
  10.  1 files changed, 1 insertions(+), 0 deletions(-)  
  11. [root@www KEY]# git checkout master    //dev 分支工作已经完成,切换到master分支  
  12. Switched to branch 'master'  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git merge dev   //合并分支,master向前移动  
  2. Updating 7050e08..86a10c8  
  3. Fast-forward  
  4.  test.c |    1 +  
  5.  1 files changed, 1 insertions(+), 0 deletions(-)  
  6. [root@www KEY]# cat test.c  
  7. #include<stdio.h>  
  8. int main()  
  9. {  
  10.     printf("we are young\n");  
  11.     printf("I love you\n");  
  12.     return 0;  
  13. }  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git branch -d dev //删除分支  
  2. Deleted branch dev (was 86a10c8).  

(2)分支冲突

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git checkout -b feature1  //创建分支  
  2. Switched to a new branch 'feature1'  
  3. [root@www KEY]# vim test.c    //修改test.c  
  4. [root@www KEY]# cat test.c  
  5. #include<stdio.h>  
  6. int main()  
  7. {  
  8.     printf("we are young\n");  
  9.     return 0;  
  10. }  
  11. [root@www KEY]# git add test.c   //添加并提交到仓库  
  12. [root@www KEY]# git commit -m "modify"  
  13. [feature1 341a39d] modify  
  14.  1 files changed, 0 insertions(+), 1 deletions(-)  
  15. [root@www KEY]# git checkout master  //切换分支到master  
  16. Switched to branch 'master'  
  17. Your branch is ahead of 'origin/master' by 1 commit.  
  18. [root@www KEY]# vim test.c   //修改test.c  
  19. [root@www KEY]# cat test.c  
  20. #include<stdio.h>  
  21. int main()  
  22. {  
  23.     printf("we are young\n");  
  24.     printf("I love you\n");  
  25.     printff("love you");  
  26.     return 0;  
  27. }  
  28. [root@www KEY]# git add test.c    //添加并提交到仓库  
  29. [root@www KEY]# git commit -m "modify"  
  30. [master 8de8474] modify  
  31.  1 files changed, 1 insertions(+), 0 deletions(-)  
  32. [root@www KEY]# git merge feature1 //合并分支  
  33. Auto-merging test.c  
  34. CONFLICT (content): Merge conflict in test.c   //冲突  
  35. Automatic merge failed; fix conflicts and then commit the result.  
  36. [root@www KEY]# git status   //查看仓库状态  
  37. # On branch master  
  38. # Your branch is ahead of 'origin/master' by 2 commits.  
  39. #  
  40. # Unmerged paths:  
  41. #   (use "git add/rm <file>..." as appropriate to mark resolution)  
  42. #  
  43. #   both modified:      test.c  
  44. #  
  45. # Untracked files:  
  46. #   (use "git add <file>..." to include in what will be committed)  
  47. #  
  48. #   gitskills1/  
  49. no changes added to commit (use "git add" and/or "git commit -a")  
  50. [root@www KEY]# cat test.c  
  51. #include<stdio.h>  
  52. int main()  
  53. {  
  54.     printf("we are young\n");  
  55. <<<<<<< HEAD  
  56.     printf("I love you\n");  
  57.     printff("love you");  
  58. =======  
  59. >>>>>>> feature1  
  60.     return 0;  
  61. }  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@www KEY]# git add test.c   //再次提交到仓库,将master和head向前移动一个时间点  
  2. [root@www KEY]# git commit -m "conflict fixed"  
  3. [master aa9d39c] conflict fixed  
  4. [root@www KEY]# git merge feature1    //合并分支  
  5. Already up-to-date.  
  6. [root@www KEY]#  git log --graph --pretty=oneline --abbrev-commit  //查看合并情况  
  7. *   aa9d39c conflict fixed  
  8. |\    
  9. | * 341a39d modify  
  10. * | 8de8474 modify  
  11. |/    
  12. * 86a10c8 modify  
  13. * 7050e08 modify 2 file  
  14. * 2ff3615 add a file  
  15. [root@www KEY]# git branch -d feature1  //删除分支  
  16. Deleted branch feature1 (was 341a39d).  




十、解决本地仓库与远程仓库的冲突
(1)git fetch: 取回远程主机某个分支的更新,再与本地的指定分支合并
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost BinaryTree]# git fetch origin master  //从远程的origin的master主分支下载最新的版本到origin/master分支上  
  2. [root@localhost BinaryTree]# git log -p master..origin/master  //比较本地的master分支和origin/master分支的差别  
  3. [root@localhost BinaryTree]# git merge origin/master  //进行合并  

或者这样更清晰
git fetch origin master:tmp
git diff tmp 
git merge tmp
(2)git pull

git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。

$ git pull <远程主机名> <远程分支名>:<本地分支名>

比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。

$ git pull origin next:master

如果远程分支是与当前分支合并,则冒号后面的部分可以省略。

$ git pull origin next

上面命令表示,取回origin/next分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。

$ git fetch origin$ git merge origin/next

在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。

Git也允许手动建立追踪关系。

git branch --set-upstream master origin/next

上面命令指定master分支追踪origin/next分支。

如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名。

$ git pull origin

上面命令表示,本地的当前分支自动与对应的origin主机”追踪分支”(remote-tracking branch)进行合并。

如果当前分支只有一个追踪分支,连远程主机名都可以省略。



git pull命令表示,当前分支自动与唯一一个追踪分支进行合并。

如果合并需要采用rebase模式,可以使用–rebase选项。

$ git pull --rebase <远程主机名> <远程分支名>:<本地分支名>
0 0
原创粉丝点击