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


当前回答

如果尚未在服务器上推送更改,则可以使用

git reset --soft HEAD~1

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

如果您已经推动了更改,请按照@CharlesB回答的步骤操作

其他回答

git checkout HEAD~ path/to/file
git commit --amend

以下内容将显示您想要的文件,这是OP要求的。

git reset HEAD^ /path/to/file

您将看到以下内容。。。

要提交的更改:(使用“git reset HEAD…”取消页面)修改:/path/to/file未提交的更改:(使用“git add…”更新将提交什么)(使用“gitcheckout--…”放弃工作目录中的更改)修改:/path/to/file

“要提交的更改”是提交之前文件的先前版本。如果文件从未存在,这看起来像是删除。如果您提交此更改,则会有一个修订将更改还原为分支中的文件。“未提交的更改”是您提交的更改,以及文件的当前状态

此时,您可以对文件执行任意操作,例如重置为不同的版本。

当您准备好承诺时:

git commit --amend -a

或者(如果您还有其他不想提交的更改)

git commit add /path/to/file
git commit --amend

我将当前文件复制到另一个文件夹中,然后通过以下方式删除所有未推送的更改:

git reset --hard @{u}

然后把东西复制回去。提交,推。

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

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