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


当前回答

最好的方法是使用SourceTree(也可以根据需要从命令行执行),并且:

检查新分支机构(出于安全原因)然后在要更改的提交之前使用gitreset-soft进行提交它将允许您删除(暂存/取消暂存)不需要的文件:)

其他回答

git reset --soft HEAD~1. 

这将撤消本地回购中的最后一次提交,并将所有内容移回提交前的阶段区域。然后,只需正常使用任何Git UI工具(如TortoiseGit、Git UI、Git Extensions…)来取消我们不想提交的文件的页面,然后再次提交。

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

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

我将用例子向你解释。设A、B、C为3次连续提交。提交B包含一个不应提交的文件。

git log  # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>    
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)

正如接受的答案所示,您可以通过重置整个提交来实现这一点。但这是一种相当严厉的做法。要做到这一点,一个更干净的方法是保留提交,只需从中删除更改的文件。

git reset HEAD^ -- path/to/file
git commit --amend --no-edit

git reset会将文件恢复为上次提交时的状态,并将其存储在索引中。工作目录中的文件未被触动。然后,gitcommit将提交并将索引压缩到当前提交中。

这基本上会获取上一次提交中的文件版本,并将其添加到当前提交中。这不会导致任何净更改,因此文件有效地从提交中删除。