try git 的一些学习笔记

昨天由江阁介绍,发现了一个在线试用 git 的好地方,http://try.github.com/levels/1…,今天重新做一遍,同时当作笔记记录下来:

  1. 首先,使用 git init 命令来初始化 git 仓库,这个命令的主要作用是建立 .git 隐藏文件夹,里面存放着 git 本身跟踪用的一些文件,具体细节没有探究。
  2. 我们随时可以使用 git status 来查看当前的仓库状态。
  3. 当我们新增了一个文件之后,在 git status 中会得到
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "­git add <file>..." to­ include i­n what wil­l be commi­tted)
    #
    #	octocat.txt
    nothing ad­ded to com­mit but un­tracked fi­les presen­t (use "gi­t add" to ­track)
    

    表示文件没有处于被跟踪的状态,也就是无法进行版本控制

  4. 那么这个时候我们应该是用 git add octocat.txt 来把文件纳入版本库,之后可以得到
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #	new file:   octocat.txt
    #
    
  5. 然后 git commit -m “Add cute octocat story” 来提交文件到本地版本库
  6. 使用 git add ‘*.txt’ 来添加多个文件
  7. 然后再次提交,git commit -m ‘Add all the octocat txt files’
  8. 使用 git log 来查看日志记录

    commit 1ed21fdf91684c6f35300acde109205f4f398075
    Author: Try Git <try_git@github.com>
    Date:   Wed Aug 8 09:01:12 2012 +0000
    
        Add all the octocat txt files
    
    commit d8e36726c76410640149058c4644d6021a3f0a40
    Author: Try Git <try_git@github.com>
    Date:   Wed Aug 8 09:01:12 2012 +0000
    
        Added cute octocat story
    
  9. 使用 git remote add origin git@github.com:ZRJ/try_git.git 来添加远程版本库
  10. 使用 git push -u origin master 把本地的库推送到服务器上,这里的参数需要了解一下

    The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do. Go ahead and push it!

  11. 然后,假设有人修改了服务器上的版本,我们需要更新本地的库,git pull origin master
  12. 可以使用 git diff HEAD 来查看被修改的部分

    diff --git a/octocat.txt b/octocat.txt
    index 7d8d808..e725ef6 100644
    --- a/octocat.txt
    +++ b/octocat.txt
    @@ -1 +1 @@
    -A Tale of Two Octocats
    +A Tale of Two Octocats and an Octodog
    
  13. 然后再往本地库里面添加一个我们的文件,git add octofamily/octodog.txt
  14. 使用 git diff –staged 来查看未提交的修改

    diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
    new file mode 100644
    index 0000000..cfbc74a
    --- /dev/null
    +++ b/octofamily/octodog.txt
    @@ -0,0 +1 @@
    +woof
    
  15. 使用 git reset octofamily/octodog.txt 来移除文件,但是文件依然在硬盘上,在执行这个命令之前,git status 是这样的

    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	new file:   octofamily/octodog.txt
    #
    # Changes not staged for commit:
    #   (use "­git add <file>..." to­ update wh­at will be­ committed­)
    #   (use "­git checko­ut -- <file>..." to­ discard c­hanges in ­working di­rectory)
    #
    #	modified:   octocat.txt
    #
    

    执行之后的 git status 是

    # On branch master
    # Changes not staged for commit:
    #   (use "­git add <file>..." to­ update wh­at will be­ committed­)
    #   (use "­git checko­ut -- <file>..." to­ discard c­hanges in ­working di­rectory)
    #
    #	modified:   octocat.txt
    #
    # Untracked files:
    #   (use "­git add <file>..." to­ include i­n what wil­l be commi­tted)
    #
    #	octofamily/octodog.txt
    

    官方文档也有说

    git reset did a great job of unstaging octodog.txt, but you’ll notice that he’s still there. He’s just not staged anymore. It would be great if we could go back to how things were before octodog came around and ruined the party.

  16. 使用 git checkout — octocat.txt 来恢复文件到最后一次提交的状态
  17. 使用 git branch clean_up 来建立一个新的分支,紧接着使用 git checkout clean_up 来切换到新的分支上
  18. 在新的分支中删除所有文件,git rm ‘*.txt’ 这里使用 git 的 rm 来删除

    git rm command which will not only remove the actual files from disk, but will also stage the removal of the files for us.

    删除之后的 git status

    # On branch clean_up
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	deleted:    blue_octocat.txt
    #	deleted:    octocat.txt
    #	deleted:    octofamily/baby_octocat.txt
    #	deleted:    octofamily/momma_octocat.txt
    #	deleted:    red_octocat.txt
    #
    # Untracked files:
    #   (use "­git add <file>..." to­ include i­n what wil­l be commi­tted)
    #
    #	octofamily/
    
  19. 然后提交修改 git commit -m “Remove all the cats”
  20. 切换到主分支,git checkout master,注意到,切换到主分支后,那些 txt 文件又出现了
  21. 把清理的分支合并到当前的分支上,git merge clean_up

    Updating f625815..ec52ea9
    Fast-forward
     blue_octocat.txt             | 1 -
     octocat.txt                  | 1 -
     octofamily/baby_octocat.txt  | 1 -
     octofamily/momma_octocat.txt | 1 -
     red_octocat.txt              | 1 -
     5 files changed, 5 deletions(-)
     delete mode 100644 blue_octocat.txt
     delete mode 100644 octocat.txt
     delete mode 100644 octofamily/baby_octocat.txt
     delete mode 100644 octofamily/momma_octocat.txt
     delete mode 100644 red_octocat.txt
    
  22. 删除清理的分支,git branch -d clean_up
  23. 推送到远程服务器上,git push

Leave a Reply

Your email address will not be published. Required fields are marked *