windows下git的安装、配置与操作(五)——文件的修改与删除

Git跟踪并管理的是修改,而非文件。你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。

我们对readme.txt进行修改加入一行内容:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
windowsÏÂgitµİ²װ¡¢ÅäÖÃÓë²Ù×÷
we are young!
然后添加:

$ 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文件:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
windowsÏÂgitµİ²װ¡¢ÅäÖÃÓë²
we are young!yes we are young!
然后我们提交文件:

$ git commit -m "added we are young"
[master b48f2fc] added we are young
 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")

查看不同:

diff --git a/readme.txt b/readme.txt
index d5c3ceb..1547332 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,4 +2,4 @@ Hello World!
 this is a git file.
 this file will commit to git.
 windows<CF><C2>git<B5>İ<B2>װ<A1><A2><C5><E4><D6><C3><D3><EB><B2><D9><D7><F7>
-we are young!
\ No newline at end of file
+we are young!yes we are young!
\ No newline at end of file
你可以继续 git addgit commit,也可以别着急提交第一次修改,先 git add第二次修改,再 git commit,就相当于把两次修改合并后一块提交了:

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

如果已经修改了工作区文件还没有添加到暂存区。那么可以通过git checkout -- file丢掉工作区的修改:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
this is a wrong line.
我们查看文件的状态:

$ 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已经给与了我们提示。
use "git checkout -- <file>..." to discard changes in working directory
用git checkout指令可以覆盖本地的修改。我们来试一下:

$ git checkout -- readme.txt
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
我们发现错误的一行已经清除了。
如果我们有错误并且已经add到了暂存区去的话。需要另外的操作:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
this is a wrong line and we added it to git.
$ git add readme.txt
查看文件状态:

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

        modified:   readme.txt
git同样给出了提示。
use "git reset HEAD <file>..." to unstage
$ git reset HEAD readme.txt
Unstaged changes after reset:
M       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")
先使用git reset HEAD将暂存区的文件回退到工作区的状态 然后使用刚才的git checkout删除修改。
$ git checkout -- readme.txt

$ git status
On branch master
nothing to commit, working directory clean

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
这样我们的修改就撤销啦。

如果已经commit到版本库还没有推送到远程。那同样可以使用版本回退。

如果推送到远程就没法补救啦。


文件删除

我们在工作区新建一个文件:

$ git add hello.txt
$ git commit -m "add hello.txt"
[master c1a0bcb] add hello.txt
 1 file changed, 2 insertions(+)
 create mode 100644 hello.txt
$ cat hello.txt
hello world!
你好 世界!
现在我们删除这个文件:

$ 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:    hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
然后删除版本库的文件:

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

        deleted:    hello.txt

参考文章:管理修改 撤销修改 删除文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值