Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

git - 从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?

(如何从上次提交中删除文件?)

  ask by Mojoy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...