本文翻译自:Unstage a deleted file in git

Usually, to discard changes to a file you would do: 通常,要放弃对文件的更改,您将执行以下操作:

git checkout -- <file>

What if the change I want to discard is deleting the file? 如果我要丢弃的更改是删除文件怎么办? The above line would give an error: 以上行会出错:

error: pathspec '<file>' did not match any file(s) known to git.

What command will restore that single file without undoing other changes? 什么命令将恢复该单个文件而不撤消其他更改?

bonus point: Also, what if the change I want to discard is adding a file? 奖励点:另外,如果我要丢弃的更改是添加文件怎么办? I would like to know how to unstage that change as well. 我想知道如何取消这种变化。


#1楼

参考:https://stackoom.com/question/eFA7/在git中取消删除已删除的文件


#2楼

If it has been staged and committed, then the following will reset the file: 如果已经暂存并提交,则以下内容将重置文件:

git reset COMMIT_HASH file_path
git checkout COMMIT_HASH file_path
git add file_path

This will work for a deletion that occurred several commits previous. 这将适用于之前发生过几次提交的删除。


#3楼

Both questions are answered in git status . 这两个问题都以git status回答。

To unstage adding a new file use git rm --cached filename.ext 要取消添加新文件,请使用git rm --cached filename.ext

# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   test

To unstage deleting a file use git reset HEAD filename.ext 要取消暂停删除文件,请使用git reset HEAD filename.ext

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    test

In the other hand, git checkout -- never unstage, it just discards non-staged changes. 另一方面, git checkout --永远不会失败,它只会丢弃非分阶段的变化。


#4楼

The answers to your two questions are related. 你的两个问题的答案是相关的。 I'll start with the second: 我将从第二个开始:

Once you have staged a file (often with git add , though some other commands implicitly stage the changes as well, like git rm ) you can back out that change with git reset -- <file> . 一旦你上传了一个文件(通常使用git add ,虽然其他一些命令也隐含地对这些更改进行了调整,比如git rm )你可以使用git reset -- <file>来取消该更改。

In your case you must have used git rm to remove the file, which is equivalent to simply removing it with rm and then staging that change. 在你的情况下,你必须使用git rm删除文件,这相当于简单地用rm删除它然后暂存该更改。 If you first unstage it with git reset -- <file> you can then recover it with git checkout -- <file> . 如果您首先使用git reset -- <file>取消它,则可以使用git checkout -- <file>恢复它。


#5楼

Assuming you're wanting to undo the effects of git rm <file> or rm <file> followed by git add -A or something similar: 假设你想要撤消git rm <file>rm <file>然后是git add -A或类似的东西:

# this restores the file status in the index
git reset -- <file>
# then check out a copy from the index
git checkout -- <file>

To undo git add <file> , the first line above suffices, assuming you haven't committed yet. 要撤消git add <file> ,假设尚未提交,则上面的第一行就足够了。

在git中取消删除已删除的文件相关推荐

  1. 不小心在git中commit了过大的文件怎么办

    由于在做lab5的时候不小心把内存的dump文件复制到了工程里并且commit了,之后我虽然删除了本地的文件,但是push的时候仍然提示我文件过大.经过查找资料,我发现了如下解决办法. 首先我试过直接 ...

  2. Git从库中移除已删除大文件

    写在前面 大家一定遇到过在使用Git时,不小心将一个很大的文件添加到库中,即使删除,记录中还是保存了这个文件.以后不管是拷贝,还是push/pull都比较麻烦.今天在上传工程到github上,发现最大 ...

  3. Linux 系统中成功恢复已删除的文件

    点击上方"朱小厮的博客",选择"设为星标" 后台回复"书",获取 后台回复"k8s",可领取k8s资料 当用户意外地删除 ...

  4. 制作强制删除文档 删除已损坏的文件

    使用文本文档来添加一些指令并保存为bat文档,便可轻松删除无法删除的文件.文件夹. 1. 右键单击电脑桌面,点击"新建"并选择"文本文档". 2.把以下命令复制 ...

  5. git 删除已add的文件夹_如何删除还没有被add到git的文件(git clean的用法)

    如何删除还没有被add到git的文件(git clean的用法) git clean命令用来从你的工作目录中删除所有没有tracked过的文件 git clean经常和git reset --hard ...

  6. Eclipse中如何恢复已删除文件

    http://tech.ddvip.com/2008-12/122855149998075.html http://zhangjunhd.blog.51cto.com/ 如果,在开发中,(Eclips ...

  7. python history文件_从Python脚本中取消设置或删除.bash_history的最新版本

    问题 我有一个Python脚本,当我从命令行运行它时,我不想在.bash_history中记录任何内容. 这样做的原因是该脚本使用Python argparse库,该库允许我直接从命令行将参数传递给p ...

  8. git 删除 已提交的 文件

    如果不想要的文件夹提交到了GIT 这时想要删除相关的文件则可以使用以下命令 删除 target 文件夹 git rm -r target git commit -m "删除文件" ...

  9. git 配置.gitignore 删除已提交的文件

    提交了才发现多提交了一些文件,希望远程把这些文件删掉 解决方法 配置.gitignore,把不想提交的内容写进去 清空缓存后提交 git rm --cached -r . git add . git ...

最新文章

  1. oracle 树状结构一直出现不了_SEO站内优化:网站结构优化(十一)
  2. MYSQL 查看当前用户与数据库版本
  3. 用twisted为未来安排任务(Scheduling tasks for the future
  4. android fitsSystemWindows的使用
  5. C#如何获得系统时间
  6. Super-palindrome【字符串+思维】
  7. [LeetCode]Power
  8. 工作六年的前端开发在想什么
  9. 【图像加密】基于matlab混沌算法图像加密解密【含Matlab源码 1218期】
  10. Spine动画打包成AssetBundle资源到安卓平台时,材质丢失的问题解决方案
  11. 【案例】中国城市规划设计研究院:新型城镇化监控与评估平台
  12. 干货分享:常见的十种破解密码方法!赶紧收藏!
  13. Ubuntu系统的百度网盘网络错误导致无法上传和下载文件问题解决方法
  14. 大龄程序猿的出路在哪里
  15. word添加水印后多出一条线的删除方法
  16. 激战和ErgoDex DX1
  17. 网易严选 DMP 标签系统建设实践
  18. 店铺推广前准备工作有哪些?
  19. git撤销的相关操作
  20. Java面向对象高阶

热门文章

  1. 3-unit8 Mariadb数据库
  2. PHP获得文件的md5并检验是否被修改
  3. 每日英语:Three Shows That Changed The Way Networks Think About Viewership
  4. C++中的构造函数小结
  5. 解决:能提交和更新,但SVN查看log时提示找不到路径'svn/XXXX'
  6. Oracle数据库重复数据删除的三种情况
  7. WebBrowser页面与WinForm交互技巧(转)
  8. APP审核关于3.2.1金融资格的审核回复苹果
  9. 暑期OI大电影——不看后悔整个OI生涯!
  10. Redis 存储SQL表格 方法