git学习03 - 撤销修改&删除文件

时间:2023-03-09 19:51:15
git学习03 - 撤销修改&删除文件

撤销修改:git checkout -- filename ;将工作区文件回到最近一次add 或者 commit的状态

撤销修改分为三种情况:

1、未提交至暂存区

  使用git checkout -- filename 将master的文件替换到工作区

2、已提交至暂存区

① 使用git reset HEAD filename 撤销暂存区的修改,恢复暂存区的文件与master分支一致

② 再使用git checkout -- filename 将暂存区的文件替换到工作区

3、已提交至master分支

① 使用git reset --hard commit-id 回退版本至修改前

② 使用git checkout -- filename 将master分支文件替换到工作区

D:\learn\git_test>git checkout -- readme.txt

删除文件:git rm filename

D:\learn\git_test>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.txt

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

        git
        type

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

D:\learn\git_test>git rm readme.txt
rm 'readme.txt'

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

        deleted:    readme.txt

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

        git
        type

D:\learn\git_test>git commited -m "delete a file"
git: 'commited' is not a git command. See 'git --help'.

The most similar command is
        commit-tree

D:\learn\git_test>git commit -m "delete a file"
[master 8b89ced] delete a file
  file changed,  deletions(-)
 delete mode  readme.txt

D:\learn\git_test>git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        git
        type

nothing added to commit but untracked files present (use "git add" to track)

D:\learn\git_test>

若想撤销删除,则也可使用git checkout -- filename 将版本库中文件替换到工作区