如何从最新提交中删除文件?


当前回答

git rm  -r ./* --cached

这将删除所有缓存/暂存文件

其他回答

事实上,我认为一种更快更简单的方法是使用git rebase交互模式。

git rebase -i head~1  

(或头~4,你想走多远)

然后,使用“edit”代替“pick”。我没有意识到“编辑”有多强大。

https://www.youtube.com/watch?v=2dQosJaLN18

希望你会发现它有帮助。

执行以下命令序列:

//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 filter-branch --prune-empty --index-filter 'git rm --ignore-unmatch --cached "file_to_be_removed.dmg"'

如果看到此错误:

无法创建新备份。refs/original中已存在以前的备份/强制使用-f覆盖备份

只需删除本地存储库上的refs备份

$ rm -rf .git/refs/original/refs
Here is the step to remove files from Git Commit.

>git reset --soft HEAD^1(either commitid ) -- now files moved to the staging area.
>git rm --cached filename(it will removed the file from staging area)
>git commit -m 'meaningfull message'(Now commit the required files)