美文网首页2017届西安思沃大讲堂程序员Git简单应用
Git:修改/放弃修改;删除/放弃删除

Git:修改/放弃修改;删除/放弃删除

作者: Josaber | 来源:发表于2016-12-05 17:07 被阅读4176次

    原始文件README.MD:

    ## README.MD
    
    * This a readme file by Joshuaber.
    * This file is used to study git.
    * Time: 2016-12-05
    
    > Git is a free distributed version control system.
    

    修改文件

    首先我们查看一下仓库状态:

    ➜  Git git:(master) git status
    On branch master
    nothing to commit, working tree clean
    

    我们做出这样的修改:

    ## README.MD
    ... ...
    

    仅仅在原始文件中加了个.MD。再查看一下状态:

    ➜  Git git:(master) ✗ 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.MD
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    ➜  Git git:(master) ✗ git add .
    

    此时我们再一次修改文件,然后查看状态:

    ➜  Git git:(master) ✗ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   README.MD
    
    ➜  Git git:(master) ✗ git commit -m "add MD"
    [master 69611e0] add MD
     1 file changed, 1 insertion(+), 1 deletion(-)
    

    提交后在查询状态,发现分支上还有改变,说明第二次改变并没有被提交:

    ➜  Git git:(master) ✗ 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.MD
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ➜  Git git:(master) ✗ 
    

    也就是说:修改->add->修改->commit只能commit已经add的修改。
    若要保存第二次修改需要再一次add然后commit

    放弃修改

    不知道大家有没有注意到:在上述git commit结果中有这样一句
    (use "git checkout -- <file>..." to discard changes in working directory)
    即:git checkout --<file>可以丢弃工作区的修改

    命令git checkout -- README.MD就是,把README.MD在工作区的修改全部撤销,这里有两种情况:

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

    总之,让这个文件回到最近一次git commitgit add时的状态
    git checkout -- <file>命令中的--很重要,没有就变成了“切换分支”的命令。

    此时我们查看文件的内容,果然回到了修改前的内容。


    上面说的是丢弃工作区的修改,如果修改完后已经git add了之后想撤销修改怎么办?

    悲伤

    好,我们来试一下:首先修改文件,然后执行命令

    ➜  Git git:(master) ✗ git add .
    ➜  Git git:(master) ✗ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   README.MD
    
    ➜  Git git:(master) ✗ 
    

    值得高兴的是,我们看到这样一句话:
    (use "git reset HEAD <file>..." to unstage)
    那我们来做一下:

    ➜  Git git:(master) ✗ git reset HEAD README.MD
    Unstaged changes after reset:
    M   README.MD
    

    我们来查询一下状态:

    ➜  Git git:(master) ✗ 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.MD
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ➜  Git git:(master) ✗ 
    

    工作区有修改,暂存区已经干净了。那如何撤销工作取得修改呢?
    git checkout -- README.MD

    删除文件

    在Git中,删除也是修改。我们可以这样做:

    • 直接在文件管理器中把没用的文件删了,或者用rm命令rm <file>
    • Git知道你删除了文件,工作区和版本库就不一致了,查看状态:git status
    ➜  Git git:(master) rm README.MD
    ➜  Git git:(master) ✗ 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:    README.MD
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ➜  Git git:(master) ✗ 
    

    你有两个选择:

    • 确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit
    git rm README.MD
    git commit -m "remove README.MD"
    
    • 另一种情况是删错了,可以把误删的文件恢复到最新版本:
    ➜  Git git:(master) ✗ git checkout -- README.MD 
    ➜  Git git:(master) ls
    README.MD
    

    记住:git checkout命令就是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除。

    撤回撤回

    相关文章

      网友评论

      • Hello_kid:git删除文件,就是我用rm命令删除一次, 不能直接 add 和 commit ,

        我需要git rm <file> 再commit 是这个意思吗
        Josaber:这两个rm是不一样的,rm命令是你删除文件可以add和提交,git rm命令删除文件是指git不管理你这个文件了,但是文件还在
      • 鸭梨山大哎:这个怎么破
        error: failed to push some refs to 'https://github.com/yingxincui/demo.git'
        hint: Updates were rejected because the remote contains work that you do
        hint: not have locally. This is usually caused by another repository pushing
        hint: to the same ref. You may want to first integrate the remote changes
        hint: (e.g., 'git pull ...') before pushing again.
        Josaber:@鸭梨山大哎 你在push之前pull一下,建议用rebase方式,处理好冲突之后,再push看看

      本文标题:Git:修改/放弃修改;删除/放弃删除

      本文链接:https://www.haomeiwen.com/subject/fbzkmttx.html