zoukankan      html  css  js  c++  java
  • 3. Git如何管理修改

    管理修改

    Git跟踪并管理修改,而非文件。

    做实验验证。第一步,对readme.txt做一个修改,比如加一行内容:

    $ cat readme.txt
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes.
    

    然后通过git add提交修改,通过git status查看工作区状态:

    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   readme.txt
    
    

    然后,再修改readme.txt:

    $ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    

    提交:

    $ git commit -m "git tracks changes"
    [master 8c299f0] git tracks changes
     1 file changed, 1 insertion(+)
    

    提交后,再看看状态:

    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   readme.txt
                
    no changes added to commit (use "git add" and/or "git commit -a")
    

    可以看出,第二次修改没有被提交

    回顾操作过程:

    第一次修改 -> git add -> 第二次修改 -> git commit

    Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。

    提交后,用git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别:

    $ git diff HEAD -- readme.txt
    diff --git a/readme.txt b/readme.txt
    index db28b2c..9a8b341 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,4 +1,4 @@
     Git is a distributed version control system.
     Git is free software distributed under the GPL.
     Git has a mutable index called stage.
    -Git tracks changes.
     No newline at end of file
    +Git tracks changes of files.
     No newline at end of file
    

    可见,第二次修改确实没有被提交。

    小结

    每次修改,如果不用git add到暂存区,那就不会加入到commit中。

  • 相关阅读:
    js修改div标签中的内容
    echarts如何显示在页面上
    mybatis提取<where><if>共用代码
    部署LAMP-LAMP平台集成
    PHP安装指南
    部署LAMP-mysql 安装
    apache虚拟主机
    apache默认网站
    HDU 5375 Gray code 格雷码(水题)
    HDU 5371 Hotaru's problem (Manacher,回文串)
  • 原文地址:https://www.cnblogs.com/BigMario/p/13576167.html
Copyright © 2011-2022 走看看