2017届西安思沃大讲堂程序员Git简单应用

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

2016-12-05  本文已影响4176人  Josaber

原始文件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中,删除也是修改。我们可以这样做:

➜  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 README.MD
git commit -m "remove README.MD"
➜  Git git:(master) ✗ git checkout -- README.MD 
➜  Git git:(master) ls
README.MD

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

撤回撤回
上一篇下一篇

猜你喜欢

热点阅读