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


当前回答

事实上,我认为一种更快更简单的方法是使用git rebase交互模式。

git rebase -i head~1  

(或头~4,你想走多远)

然后,使用“edit”代替“pick”。我没有意识到“编辑”有多强大。

https://www.youtube.com/watch?v=2dQosJaLN18

希望你会发现它有帮助。

其他回答

我将用例子向你解释。设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 reset--soft HEAD ^返回您的提交,当您键入git status时,它会告诉您要做什么:

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

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

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

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

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

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

$ git commit --amend // or stash and rebase to <commit_id> to amend changes
git checkout HEAD~ path/to/file
git commit --amend