0
点赞
收藏
分享

微信扫一扫

【Git】Git撤销add操作

Git add错了文件怎么办?可以查看以下两篇
​​​https://git-scm.com/book/zh/v1/Git-基础-记录每次更新到仓库​​​​https://git-scm.com/book/zh/v1/Git-基础-撤消操作​​

我们来实践一下

查看Git状态

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .idea/misc.xml
modified: .idea/modules.xml
modified: app/app.iml

只要在 “Changes to be committed” 这行下面的,就说明是已暂存状态。也就是说这些文件被​​add​​​过,但是没有​​commit​​​。如果此时提交,那么该文件此时此刻的版本将被留存在历史记录中。
但是这些文件是被.ignore忽略的,所以我们应该取消​​​add​

​git reset HEAD <路径/文件名>​​ 就是对某个文件进行撤销了

测试下

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .idea/modules.xml
modified: app/app.iml

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: .idea/misc.xml

文件misc.xml 出现在 “Changes not staged for commit” 这行下面,说明已跟踪文件的内容发生了变化,但还没有放到暂存区。

因为.idea/misc.xml已经被.ignore忽略了,所以对它的修改完全没有必要,我们看到刚才的输出界面在第二个括号中,我们看到了抛弃文件修改的命令,试一下

$ git checkout -- .idea/misc.xml

​git reset HEAD​​ 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了

$  git reset HEAD
Unstaged changes after reset:
M .idea/modules.xml
M app/app.iml

我们再用​​git checkout -- <路径/文件名>​

$ git checkout -- .idea

这样就摒弃了.idea文件夹里的文件修改

我们可以同样操作其他文件夹,最后用​​git status​​查看下状态

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean


举报

相关推荐

0 条评论