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


当前回答

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 2.23开始,您可以简单地使用以下命令:

git restore --staged <file>

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

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

我们可以采取的另一种方法是:

删除文件使用删除的文件进行新提交执行交互式重基并压缩两个提交推

同样的问题是,我在本地分支中进行了更改,我只想恢复一个文件。对我有用的是-

(下面的feature/target_branch是我所有更改的地方,包括我想撤消的特定文件的更改)

(origin/feature/target_branch是我要将更改推送到的远程分支)

(feature/stating是我的临时暂存分支,我将从所有需要的更改中推送,不包括对该文件的更改)

从我的原点/feature/target_branch创建本地分支-称为feature/stating将我的工作本地分支feature/target_branch合并到feature/stating分支签出功能/暂存,然后git reset--soft ORIG_HEAD(现在将暂存功能/暂存的所有更改,但未提交。)取消对我以前签入的文件进行不必要的更改将feature/stating的上游分支更改为origin/feature/target_branch提交其余的阶段性更改,并向上游推送到我的远程原点/feature/target_branch

git reset—软HEAD~1git重置HEAD/file/to/deletegit rm—缓存/file/to/deletegitcommit--修改-m“您的消息”

重点是--修改

则可以:

git push-u原始主机