在执行 Git 推送命令后更改我的 git 提交中的文件

changing the files in my git commit after performing a Git Push Command

所以我们使用 GitLab,所以我不得不发送一个 mergeq 请求。正是在 mergereq 期间,我意识到我已经完成了额外文件“临时和不需要”的 git 提交;有没有办法取消这个?我试过了

git commit --amend

但这只会更改提交消息,而我还需要删除不需要的文件。

这可能吗?

不,git commit --amend 允许您对上次提交添加新的修改。它只更新提交消息的情况是当您使用 -m 参数时。

但与其尝试从索引中 rm 不需要的文件,我建议从您的本地分支*执行以下操作:

# undo last commit while keeping changes in the working tree
git reset HEAD^

# redo the adding part without the unwanted files
git add file1 file2 file3

# commit and push
git commit -m "message"
git push --force origin HEAD

现在只需刷新您的 PR 页面,它就会自行更新,用新的提交替换错误的提交。

(* 假设这是你的功能分支,你可以在不影响其他人工作的情况下强制推送。如果我在这里错了请纠正我)