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


当前回答

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

重点是--修改

则可以:

git push-u原始主机

其他回答

git rm  -r ./* --cached

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

我将用例子向你解释。设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>    

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

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

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

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

如果你不再需要那个文件,你可以

git rm file
git commit --amend
git push origin branch

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

(下面的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