zoukankan      html  css  js  c++  java
  • GIT 管理修改、删除文件

    管理修改

    现在,假定你已经完全掌握了暂存区的概念。下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。

    你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。

    为什么说Git管理的是修改,而不是文件呢?我们还是做实验。第一步,对readme.txt做一个修改,比如加一行内容:

    test git modify second
    study git
    three add
    four add

    然后,添加:

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

    然后,再修改readme.txt:

    test git modify second
    study git
    three add
    four add modify

    提交:

    $ git commit -m "four modify"
    [master de8fd65] four modify
     1 file changed, 2 insertions(+), 1 deletion(-)

    提交后,再看看状态:

    $ 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 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")

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

    注:

    -- 使用其他参数(指定文件)可以提交未保存到暂存区(unstaged)

    vi readme.txt
    git commit readme.txt -m "commit with unstaged modify" vi readme.txt
    git commit -a -m "commit all file will commit unstaged modify"

    --不使用其他参数不会提交unstaged modify

    git commit -m "commit without param will not commit unstaged modify"

    git diff HEAD -- readme.txt 工作区文件和版本库对比

    git diff -- readme.txt 工作区和暂存区对比

    那怎么提交第二次修改呢?你可以继续git addgit commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

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

    撤销修改

    自然,你是不会犯错的。不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行:

    test git modify second
    study git
    three add
    four add modify
    five add

    现在不想要five add 了。你可以删掉最后一行,手动把文件恢复到上一个版本的状态。如果用git status查看一下:

    $ 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会告诉你,git checkout -- file可以丢弃工作区的修改:

    $ git checkout -- readme.txt

    命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

    • 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
    • 一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

    总之,就是让这个文件回到最近一次git commitgit add时的状态。

    现在,看看readme.txt的文件内容:

    $ cat readme.txt
    test git modify second
    study git
    three add
    four add modify
    

    文件内容果然复原了。

    git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令。

    现在假定是凌晨3点,你不但写了一些胡话,还git add到暂存区了:

    test git modify second
    study git
    three add
    four add modify
    five add modify
    six add modify
    seven add modify
    eight add modify ...

    庆幸的是,在commit之前,你发现了这个问题。用git status查看一下,修改只是添加到了暂存区,还没有提交:

    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   readme.txt
    
    
    Administrator@PC-20160420ZEST MINGW32 /f/studygit (master)

    Git同样告诉我们,用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:

    $ git reset HEAD readme.txt
    Unstaged changes after reset:
    M       readme.txt

    git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。

    再用git status查看一下,现在暂存区是干净的,工作区有修改:

    $ 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 checkout -- readme.txt
    $ git status
    On branch master
    nothing to commit, working directory clean

    小结

      场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

      场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

      场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。

    删除文件

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt(之前已经建立了)到Git并且提交。

    一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

    $ rm test.txt

    这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

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

    现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit

    $ git rm test.txt
    rm 'test.txt'
    $ git commit -m "remove test.txt"
    [master d17efd8] remove test.txt
     1 file changed, 1 deletion(-)
     delete mode 100644 test.txt

    现在,文件就从版本库中被删除了。

    另一种情况是删错了,只执行了git rm test.txt, 没有执行git commit。因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

    $ git checkout -- test.txt

    如果只执行了git rm test.txt, 也执行git commit。可通过版本会退:

    $ git reset --hard HEAD^
    HEAD is now at dd34c9a no add but commit,because use other parameter

    git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

    小结

    命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

  • 相关阅读:
    [随笔][Java][修改Maven仓库库为阿里云]
    [随笔][Java][Java 类型系统]
    MongoDB终极攻略
    SQLAlchemy攻略
    linux system函数引发的错误
    QT 问题提问网站
    QT学习
    QT学习
    linux 内存
    Linux 查看内存(free)、释放内存(基本操作)
  • 原文地址:https://www.cnblogs.com/wangmingshun/p/5424659.html
Copyright © 2011-2022 走看看