git-撤销修改


工作区(Working Directory)
在电脑里能看到的目录,有一个隐藏目录.git,是Git的版本库。

版本库(Repository)

Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个支master,以及指向master的一个指针叫HEAD

把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支


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

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

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,git reset HEAD^

**************************************************************************

1)工作区jdbc.properties已更改

$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
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:   src/jdbc.properties
        modified:   src/struts.xml

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/
        cs_1221.iml
        out/

no changes added to commit (use "git add" and/or "git commit -a")

nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ cat jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/credit
#jdbc.username=root
#jdbc.password=123

#jdbc.url=jdbc:mysql://10.151.15.103:13306/credit
#jdbc.username=root
#jdbc.password=123456

#jdbc.url=jdbc:mysql://10.151.15.100:3306/intell_kefu
#jdbc.username=intell_kefu
#jdbc.password=123qwe
2)把工作区的更改,add到暂存区里
nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ git add jdbc.properties

nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   jdbc.properties

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:   struts.xml

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../.idea/
        ../cs_1221.iml
        ../out/

3)撤销add到暂存区的修改

nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ git reset jdbc.properties
Unstaged changes after reset:
M       src/jdbc.properties
M       src/struts.xml

4)暂存区的修改,继续commit到本地仓库

nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ git commit -m 'test'
[develop 8b42971] test
 1 file changed, 7 insertions(+), 3 deletions(-)

nanayin@201605050385- MINGW64 /e/IdeaProjects/CustomerService/cs_1221/src (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)
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:   struts.xml

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../.idea/
        ../cs_1221.iml
        ../out/

no changes added to commit (use "git add" and/or "git commit -a")

5)撤销本次撤销本次commit提交

git reset HEAD^

6)误删除

在工作空间中删除了一个文件,

一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit

另一种情况是删错了,把误删的文件恢复到最新版本:  git checkout -- test.txt

1.1  git reset HEAD

暂存区的目录树会被重写,会被master分支所指向的目录树所替换,但是工作区不受影响


1.2 git rm --cached <file>

会直接从暂存区删除文件,工作区不受影响


1.3 git checkout . 或者 git checkout --<file>

会用暂存区全部的文件或者指定的文件 替换工作区的文件


1.4 git checkout  HEAD .  或者 git checkout  HEAD  <file>

会用HEAD指向的master分支中的全部或者部分文件替换暂存区和工作区中的文件


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值