从Git提交中删除文件

本文翻译自:Remove files from Git commit

I am using Git and I have committed few files using 我正在使用Git,我已经提交了几个文件

git commit -a

Later, I found that a file had mistakenly been added to the commit. 后来,我发现错误地将一个文件添加到了提交中。

How can I remove a file from the last commit? 如何从上次提交中删除文件?


#1楼

参考:https://stackoom.com/question/qN2l/从Git提交中删除文件


#2楼

ATTENTION ! 注意 If you only want to remove a file from your previous commit, and keep it on disk , read juzzlin's answer just above. 如果您只想从之前的提交中删除文件并将其保存在磁盘上 ,请阅读上面的juzzlin的答案

If this is your last commit and you want to completely delete the file from your local and the remote repository , you can: 如果这是您的上次提交,并且您希望从本地和远程存储库中完全删除该文件 ,则可以:

  1. remove the file git rm <file> 删除文件git rm <file>
  2. commit with amend flag: git commit --amend 提交修改标志: git commit --amend

The amend flag tells git to commit again, but "merge" (not in the sense of merging two branches) this commit with the last commit. 修正标志告诉git再次提交,但是“合并”(不是在合并两个分支的意义上)这个提交与最后一次提交。

As stated in the comments, using git rm here is like using the rm command itself! 正如评论中所述,在这里使用git rm就像使用rm命令本身一样!


#3楼

If you have not pushed the changes on the server you can use 如果您尚未在服务器上推送更改,则可以使用

git reset --soft HEAD~1

It will reset all the changes and revert to one commit back 它将重置所有更改并恢复为一次提交

If you have pushed your changes then follow steps as answered by @CharlesB 如果您已推送更改,请按照@CharlesB所述的步骤进行操作


#4楼

Removing the file using rm will delete it! 使用rm删除文件将删除它!

You're always adding to a commit in git rather than removing, so in this instance return the file to the state it was in prior to the first commit (this may be a delete 'rm' action if the file is new) and then re-commit and the file will go. 你总是在git中添加提交而不是删除,所以在这个实例中将文件返回到第一次提交之前的状态(如果文件是新的,这可能是删除'rm'动作)然后重新提交,文件将继续。

To return the file to some previous state: 要将文件返回到以前的某个状态:

    git checkout <commit_id> <path_to_file>

or to return it to the state at the remote HEAD: 或者将其返回到远程HEAD的状态:

    git checkout origin/master <path_to_file>

then amend the commit and you should find the file has disappeared from the list (and not deleted from your disk!) 然后修改提交,你应该发现文件已从列表中消失(并且没有从磁盘中删除!)


#5楼

I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. 我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从前一次提交移回到暂存区域的问题,而不取消对它们所做的更改。 This can be done like Paritosh Singh suggested: 这可以像Paritosh Singh建议的那样完成:

git reset --soft HEAD^ 

or 要么

git reset --soft HEAD~1

Then reset the unwanted files in order to leave them out from the commit: 然后重置不需要的文件,以便将它们从提交中删除:

git reset HEAD path/to/unwanted_file

Now commit again, you can even re-use the same commit message: 现在再次提交,您甚至可以重用相同的提交消息:

git commit -c ORIG_HEAD  

#6楼

If you want to preserve your commit (maybe you already spent some time writing a detailed commit message and don't want to lose it), and you only want to remove the file from the commit, but not from the repository entirely: 如果你想保留你的提交(也许你已经花了一些时间编写一个详细的提交消息而不想丢失它),你只想从提交中删除文件,而不是完全从存储库中删除:

git checkout origin/<remote-branch> <filename>
git commit --amend
发布了0 篇原创文章 · 获赞 136 · 访问量 83万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105223841