Git教程【译】(二)

5. 远端仓库(remote repositories

5.1. 设置一个远端的Git仓库

我们将创建一个远端的Git仓库。这个仓库可以存储在本地或者是网络上。

远端Git仓库和标准的Git仓库有如下差别:一个标准的Git仓库包括了源代码和历史信息记录。我们可以直接在这个基础上修改代码,因为它已经包含了一个工作副本。但是远端仓库没有包括工作副本,只包括了历史信息。可以使用--bare选项来创建一个这样的仓库。

为了方便起见,示例中的仓库创建在本地文件系统上 

  
  
  
  
  1. # Switch to the first repository 
  2. cd ~/repo01 
  3. git clone --bare . ../remote-repository.git 
  4.  
  5. # Check the content, it is identical to the .git directory in repo01 
  6. ls ~/remote-repository.git  

5.2. 推送更改到其他的仓库

做一些更改,然后将这些更改从你的第一个仓库推送到一个远端仓库 

  
  
  
  
  1. # Make some changes in the first repository 
  2. cd ~/repo01  
  3.  
  4. # Make some changes in the file 
  5. echo "Hello, hello. Turn your radio on" > test01 
  6. echo "Bye, bye. Turn your radio off" > test02  
  7.  
  8. # Commit the changes, -a will commit changes for modified files 
  9. # but will not add automatically new files 
  10. git commit -a -m "Some changes" 
  11.   
  12. # Push the changes 
  13. git push ../remote-repository.git  

5.3. 添加远端仓库

除了通过完整的URL来访问Git仓库外,还可以通过git remote add命令为仓库添加一个短名称。当你克隆了一个仓库以后,origin表示所克隆的原始仓库。即使我们从零开始,这个名称也存在。

  
  
  
  
  1. # Add ../remote-repository.git with the name origin 
  2. git remote add origin ../remote-repository.git  
  3.  
  4. # Again some changes 
  5. echo "I added a remote repo" > test02 
  6. # Commit 
  7. git commit -a -m "This is a test for the new remote origin" 
  8. # If you do not label a repository it will push to origin 
  9. git push origin 


5.4. 显示已有的远端仓库

通过以下命令查看已经存在的远端仓库 

  
  
  
  
  1. # Show the existing defined remote repositories 
  2. git remote  

5.5. 克隆仓库

通过以下命令在新的目录下创建一个新的仓库 

 

  
  
  
  
  1. # Switch to home 
  2. cd ~ 
  3. # Make new directory 
  4. mkdir repo02 
  5.  
  6. # Switch to new directory 
  7.  
  8. cd ~/repo02 
  9. # Clone 
  10. git clone ../remote-repository.git .  

5.6. 拉取(Pull)更改

通过拉取,可以从其他的仓库中获取最新的更改。在第二个仓库中,做一些更改,然后将更改推送到远端的仓库中。然后第一个仓库拉取这些更改 

  
  
  
  
  1. # Switch to home 
  2. cd ~  
  3. # Switch to second directory 
  4. cd ~/repo02 
  5. # Make changes 
  6. echo "A change" > test01 
  7. # Commit 
  8. git commit -a -m "A change" 
  9. # Push changes to remote repository 
  10. # Origin is automatically maintained as we cloned from this repository 
  11. git push origin 
  12. # Switch to the first repository and pull in the changes 
  13. cd ~/repo01 
  14. git pull ../remote-repository.git/ 
  15. # Check the changes 
  16. less test01 

6. 还原更改

如果在你的工作副本中,你创建了不想被提交的文件,你可以丢弃它。 

  
  
  
  
  1. # Create a new file with content 
  2. touch test04 
  3. echo "this is trash" > test04  
  4.  
  5. # Make a dry-run to see what would happen 
  6. # -n is the same as --dry-run 
  7. git clean -n  
  8.  
  9. # Now delete 
  10. git clean -f  

你可以提取老版本的代码,通过提交的IDgit log命令可以查看提交ID 

  
  
  
  
  1. # Switch to home 
  2. cd ~/repo01 
  3. # Get the log 
  4. git log 
  5.  
  6. # Copy one of the older commits and checkout the older revision via  译者注:checkout
  7. 后加commit id就是把commit的内容复制到index和工作副本中 
  8. git checkout commit_name 


如果你还未把更改加入到索引中,你也可以直接还原所有的更改 

  
  
  
  
  1. #Some nonsense change 
  2. echo "nonsense change" > test01 
  3. # Not added to the staging index. Therefore we can 
  4. # just checkout the old version 
  5. #译者注:checkout后如果没有commit id号,就是从index中拷贝数据到工作副本,不涉及commit部分的改变 
  6. git checkout test01 
  7. # Check the result 
  8. cat test01 
  9. # Another nonsense change 
  10. echo "another nonsense change" > test01 
  11. # We add the file to the staging index 
  12. git add test01 
  13. # Restore the file in the staging index 
  14. #译者注:复制HEAD所指commit的test01文件到index中 
  15. git reset HEAD test01 
  16. # Get the old version from the staging index 
  17. #译者注:复制index中test01到工作副本中 
  18. git checkout test01 
  19. #译者注,以上两条命令可以合并为git checkout HEAD test01 


也可以通过revert命令进行还原操作 

  
  
  
  
  1. # Revert a commit 
  2. git revert commit_name  

即使你删除了一个未添加到索引和提交的文件,你也可以还原出这个文件

  
  
  
  
  1. # Delete a file 
  2. rm test01 
  3. # Revert the deletion 
  4. git checkout test01  

如果你已经添加一个文件到索引中,但是未提交。可以通过git reset file 命令将这个文件从索引中删除 

  
  
  
  
  1. // Create a file 
  2. touch incorrect.txt 
  3. // Accidently add it to the index 
  4. git add . 
  5. // Remove it from the index 
  6. git reset incorrect.txt 
  7. // Delete the file 
  8. rm incorrect.txt 


如果你删除了文件夹且尚未提交,可以通过以下命令来恢复这个文件夹 。译者注:即使已经提交,也可以还原

  
  
  
  
  1. git checkout HEAD -- your_dir_to_restore 

 译者注:checkoutreset这两个命令的含义是不同的,可以参阅这篇文章http://marklodato.github.com/visual-git-guide/index-en.html

7. 标记

Git可以使用对历史记录中的任一版本进行标记。这样在后续的版本中就能轻松的找到。一般来说,被用来标记某个发行的版本

可以通过git tag命令列出所有的标记,通过如下命令来创建一个标记和恢复到一个标记 

  
  
  
  
  1. git tag version1.6 -m 'version 1.6' 
  2. git checkout <tag_name> 

8. 分支、合并

8.1. 分支

通过分支,可以创造独立的代码副本。默认的分支叫masterGit消耗很少的资源就能创建分支。Git鼓励开发人员多使用分支

下面的命令列出了所有的本地分支,当前所在的分支前带有*

  
  
  
  
  1. git branch  

如果你还想看到远端仓库的分支,可以使用下面的命令

  
  
  
  
  1. git branch -a  

可以通过下面的命令来创建一个新的分支

 

  
  
  
  
  1. #Syntax: git branch <name> <hash> 
  2. <hash> in the above is optional 
  3. # if not specified the last commit will be used 
  4. # If specified the corresponding commit will be used 
  5. git branch testing 
  6. # Switch to your new branch 
  7. git checkout testing 
  8. # Some changes 
  9. echo "Cool new feature in this branch" > test01 
  10. git commit -a -m "new feature" 
  11. # Switch to the master branch 
  12. git checkout master 
  13. # Check that the content of test01 is the old one 
  14. cat test01 


8.2. 合并

通过Merge我们可以合并两个不同分支的结果。Merge通过所谓的三路合并来完成。分别来自两个分支的最新commit和两个分支的最新公共commit

可以通过如下的命令进行合并 

  
  
  
  
  1. # Syntax: git merge <branch-name> 
  2. git merge testing 

 

一旦合并发生了冲突,Git会标志出来,开发人员需要手工的去解决这些冲突。解决冲突以后,就可以将文件添加到索引中,然后提交更改

8.3. 删除分支

删除分支的命令如下: 

  
  
  
  
  1. #Delete branch testing 
  2. git branch -d testing 
  3. # Check if branch has been deleted 
  4. git branch   

8.4. 推送(push)一个分支到远端仓库

默认的,Git只会推送匹配的分支的远端仓库。这意味在使用git push命令默认推送你的分支之前,需要手工的推送一次这个分支。

  
  
  
  
  1. # Push testing branch to remote repository 
  2. git push origin testing  
  3.  
  4. # Switch to the testing branch 
  5. git checkout testing  
  6.  
  7. # Some changes 
  8. echo "News for you" > test01 
  9. git commit -a -m "new feature in branch" 
  10.  
  11. # Push all including branch 
  12. git push 

通过这种方式,你可以确定哪些分支对于其他仓库是可见的,而哪些只是本地的分支  

9. 解决合并冲突

如果两个不同的开发人员对同一个文件进行了修改,那么合并冲突就会发生。而Git没有智能到自动解决合并两个修改

在这一节中,我们会首先制造一个合并冲突,然后解决它,并应用到Git仓库中

下面会产生一个合并冲突 

  
  
  
  
  1. # Switch to the first directory 
  2. cd ~/repo01 
  3. # Make changes 
  4. touch mergeconflict.txt 
  5. echo "Change in the first repository" > mergeconflict.txt 
  6. # Stage and commit 
  7. git add . && git commit -a -m "Will create merge conflict 1"  
  8.  
  9. # Switch to the second directory 
  10. cd ~/repo02 
  11. # Make changes 
  12. touch mergeconflict.txt 
  13. echo "Change in the second repository" > mergeconflict.txt 
  14. # Stage and commit 
  15. git add . && git commit -a -m "Will create merge conflict 2" 
  16. # Push to the master repository 
  17. git push  
  18.  
  19. # Now try to push from the first directory 
  20. # Switch to the first directory 
  21. cd ~/repo01 
  22. # Try to push --> you will get an error message 
  23. git push 
  24. # Get the changes 
  25. git pull origin master  

Git将冲突放在收到影响的文件中,文件内容如下: 

  
  
  
  
  1. <<<<<<< HEAD 
  2. Change in the first repository 
  3. ======= 
  4. Change in the second repository 
  5. >>>>>>> b29196692f5ebfd10d8a9ca1911c8b08127c85f8 

上面部分是你的本地仓库,下面部分是远端仓库。现在编辑这个文件,然后commit更改。另外的,你可以使用git mergetool命令

  
  
  
  
  1. # Either edit the file manually or use 
  2. git mergetool 
  3. # You will be prompted to select which merge tool you want to use 
  4. # For example on Ubuntu you can use the tool "meld" 
  5. # After  merging the changes manually, commit them 
  6. git commit -m "merged changes"   

10. 变基(Rebase

10.1. 在同一分支中应用Rebase Commit

通过rebase命令可以合并多个commit为一个。这样用户push更改到远端仓库的时候就可以先修改commit历史

接下来我们将创建多个commit,然后再将它们rebase成一个commit

  
  
  
  
  1. # Create a new file 
  2. touch rebase.txt  
  3.  
  4. # Add it to git 
  5. git add . && git commit -m "rebase.txt added to index"  
  6.  
  7. # Do some silly changes and commit 
  8. echo "content" >> rebase.txt 
  9. git add . && git commit -m "added content" 
  10. echo " more content" >> rebase.txt 
  11. git add . && git commit -m "added more content" 
  12. echo " more content" >> rebase.txt 
  13. git add . && git commit -m "added more content" 
  14. echo " more content" >> rebase.txt 
  15. git add . && git commit -m "added more content" 
  16. echo " more content" >> rebase.txt 
  17. git add . && git commit -m "added more content" 
  18. echo " more content" >> rebase.txt 
  19. git add . && git commit -m "added more content"  
  20.  
  21. # Check the git log message 
  22. git log  

我们合并最后的七个commit。你可以通过如下的命令交互的完成

  
  
  
  
  1. git rebase -i HEAD~7 

 这个命令会打开编辑器让你修改commit的信息或者 squash/ fixup最后一个信息。

Squash会合并commit信息而fixup会忽略commit信息(待理解)

10.2. Rebasing多个分支

    你也可以对两个分支进行rebase操作。如下所述,merge命令合并两个分支的更改。rebase命令为一个分支的更改生成一个补丁,然后应用这个补丁到另一分支中。

    使用mergerebase,最后的源代码是一样的,但是使用rebase产生的commit历史更加的少,而且历史记录看上去更加的线性 

 

  
  
  
  
  1. # Create new branch 
  2. git branch testing 
  3. # Checkout the branch 
  4. git checkout testing 
  5. # Make some changes 
  6. echo "This will be rebased to master" > test01 
  7. # Commit into testing branch 
  8. git commit -a -m "New feature in branch" 
  9. # Rebase the master 
  10. git rebase master 


10.3.Rebase最佳实践

    在push更改到其他的Git仓库之前,我们需要仔细检查本地分支的commit历史

   在Git中,你可以使用本地的commit。开发人员可以利用这个功能方便的回滚本地的开发历史。但是在push之前,需要观察你的本地分支历史,是否其中有些commit历史对其他用户来说是无关的

   如果所有的commit历史都跟同一个功能有关,很多情况下,你需要rebase这些commit历史为一个commit历史。

   交互性的rebase主要就是做重写commit历史的任务。这样做是安全的,因为commit还没有被push到其它的仓库。这意味着commit历史只有在被push之前被修改

    如果你修改然后push了一个已经在目标仓库中存在的commit历史,这看起来就像是你实现了一些别人已经实现的功能

你可能感兴趣的:(git,教程)