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


当前回答

一些对我有用的东西,但仍然认为应该有更好的解决方案:

$ git revert <commit_id>
$ git reset HEAD~1 --hard

只需将要放弃的更改保留在另一个提交中,检查其他人

$ git commit --amend // or stash and rebase to <commit_id> to amend changes

其他回答

我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上一次提交移回临时区域的问题,而不取消对它们所做的更改。这可以像Pariosh Singh建议的那样:

git reset --soft HEAD^ 

or

git reset --soft HEAD~1

然后重置不需要的文件,以便将它们从提交中删除(旧方法):

git reset HEAD path/to/unwanted_file

注意,自从Git 2.23.0以来,人们可以(以新的方式):

git restore --staged path/to/unwanted_file

现在再次提交,您甚至可以重复使用相同的提交消息:

git commit -c ORIG_HEAD  

执行以下命令序列:

//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

git reset --soft HEAD~1

它将重置所有更改并恢复为一次提交

如果这是您最后一次提交,并且您希望从本地和远程存储库中删除文件,请尝试以下操作:

git rm <file>
 git commit --amend

甚至更好:

先重置

git reset --soft HEAD~1

重置不需要的文件

git reset HEAD path/to/unwanted_file

再次提交

git commit -c ORIG_HEAD  

一些对我有用的东西,但仍然认为应该有更好的解决方案:

$ git revert <commit_id>
$ git reset HEAD~1 --hard

只需将要放弃的更改保留在另一个提交中,检查其他人

$ git commit --amend // or stash and rebase to <commit_id> to amend changes

这是我从比特桶回购中删除文件的工作,我最初将文件推送到分支。

git checkout origin/develop <path-to-file>
git add <path-to-file>
git commit -m "Message"
git push