git - 如何撤消 git rm

标签 git

我不小心将一个(巨大的)日志文件添加并提交到我的本地 git 存储库中,然后执行了一个 git rm(没有 --cached)。所以历史看起来像这样:

> git status

modified:  Foo.java

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

    huge_file.log

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

然后我做了git add .:

> git add .
> git status

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

    modified:  Foo.java
    new file:   huge_file.log

然后(我忽略了我已经添加了 huge_file.log)我做了一个 git commit:

> git commit -am "Foo commit"
[master 7582793] Foo commit
 1 file changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 huge_file.log

然后我意识到我已经添加并提交了huge_file.log。所以我想我可以通过执行 git rm huge_file.log 来撤消它:

> git rm huge_file.log
> git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    huge_file.log

所以在这一点上,我意识到我弄得一团糟:我无法提交并推送以掌握此更改,因为这意味着推送 huge_file.log 然后删除 huge_file.log 。不幸的是,我无法推送,因为我们的文件限制为每个文件 100 MB,而 huge_file.log 超过 1 GB。

那我现在能做什么呢?我不能简单地将 HEAD 重置为之前的提交,因为我在 Foo.java 中做了很多我不想撤消的更改。

有没有办法改变以前的提交以某种方式删除 huge_file.log

请注意,Foo.java 只是为了 StackOverflow 上更好的可读性的代表。我已经接触了大约 10 个文件并在每个文件中修改了很多行代码,我不想丢失这些更改。

我现在该怎么办?

最佳答案

如果只是在最后一次提交, 然后执行 git rm huge_file.log 然后 git commit --amend

Git 允许您对您提交的最后一个错误进行修改。字面意思。

关于git - 如何撤消 git rm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33983138/

相关文章:

git - 在 git filter-branch --subdirectory-filter 之后保留 stash

linux - Git : mysterious changes cannot be undone 的奇怪行为

git - 如何在 git 历史中查找/识别大型提交?

git - git 可以列出两个特定提交之间出现的标签吗?

Git忽略除子文件夹以外的所有内容

git - 解决 VS Code 中不显示的 merge 冲突工具栏

git - 在 git 中签名提交使用了错误的子项

Git Fetch 与 Pull : Different Results, 不确定为什么

git - 如何在版本控制系统中组织带有随机数的代码以进行分析?

macos - 找不到 bash __git_ps1 命令