如何从最新提交中删除文件?
当前回答
使用gitGUI可以简化从先前提交中删除文件的过程。
假设这不是一个共享分支,并且您不介意重写历史,那么运行:
git gui citool --amend
您可以取消检查错误提交的文件,然后单击“提交”。
该文件将从提交中删除,但将保留在磁盘上。因此,如果在错误添加文件后取消选中该文件,它将显示在未跟踪文件列表中(如果在错误修改文件后取消检查该文件,则会显示在未提交的更改列表中)。
其他回答
一些对我有用的东西,但仍然认为应该有更好的解决方案:
$ git revert <commit_id>
$ git reset HEAD~1 --hard
只需将要放弃的更改保留在另一个提交中,检查其他人
$ git commit --amend // or stash and rebase to <commit_id> to amend changes
我将当前文件复制到另一个文件夹中,然后通过以下方式删除所有未推送的更改:
git reset --hard @{u}
然后把东西复制回去。提交,推。
将文件保存在本地(从gitcommit中删除)
如果要保留文件,可以使用--cached,如下所示:
git rm --cached filename
删除本地文件(并从gitcommit中删除)
否则,如果要删除文件,只需使用:
git rm filename
执行以下命令序列:
//to remove the last commit, but preserve changes
git reset --soft HEAD~1
//to remove unneded file from the staging area
git reset HEAD `<your file>`
//finally make a new commit
git commit -m 'Your message'
git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit -m "removed unwanted file from git"
将仍然保留本地文件。如果您也不希望文件在本地,可以跳过--cached选项。
如果所有工作都在您的本地分支上,您需要在以后提交时保存该文件,就像拥有一个干净的历史记录一样,我认为一个更简单的方法是:
git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit --squash <commit_id>
git add <file_to_remove_from_commit_<commit_id>_which_added_file>
git commit -m "brand new file!"
git rebase --interactive <commit_id>^
然后,您就可以轻松地完成rebase,而无需记住更复杂的命令、提交消息或键入。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别