简体   繁体   English

从Git提交中删除文件

[英]Remove file from Git commit

I just added a file in a commit which I don't want in there. 我只是在提交中添加了一个我不想在那里的文件。 It's a 64 bit version of a Gem, and since my other development and production machines are using 32 bit versions it makes sense to have it only on the 64 bit machine. 它是Gem的64位版本,并且由于我的其他开发和生产机器都使用32位版本,因此仅在64位机器上安装它是有意义的。 I just ran this: 我只是跑了这个:

$ git add .
$ git commit -a -m "Edit sorting fixed."
[master f99324f] Edit sorting fixed.
 2 files changed, 4 insertions(+), 3 deletions(-)
 create mode 100644 vendor/cache/libv8-3.11.8.17-x86_64-darwin-12.gem

I want the changes committed but I want that gem excluded from the commit. 我要提交更改,但我要将该宝石排除在提交之外。 What should I do? 我该怎么办? I haven't pushed the changes yet. 我还没有推送更改。

git reset HEAD~1 will put the directory back to what it was before the commit. git reset HEAD~1将把目录放回提交之前的目录。 Then you can add the .gem to .gitignore (or *.gem if you don't ever want them) and do a new commit. 然后,您可以将.gem添加到.gitignore (或如果不想使用*.gem ),然后进行新的提交。

This has bitten me enough that I generally avoid git commit -a or git add ing an entire directory. 这已经让我很痛苦,以至于我通常避免git commit -agit add整个目录。 git add -u comes in real handy to add changes only in known files. git add -u非常方便,仅在已知文件中添加更改。

Reset HEAD and Commit 重置HEAD并提交

First, do a soft reset of your HEAD to the previous commit. 首先,将您的HEAD进行重置为上一次提交。 Then just add the single file you want, rather than . 然后,只需添加所需的单个文件即可,而不是. which will add all the files in the working tree that aren't being ignored. 它将在工作树中添加所有未被忽略的文件。

git reset HEAD^
git add <just the file you want to commit>
git commit -m "Edit sorting fixed."

If you want to prevent certain files from being added by . 如果您想防止某些文件被添加. in the future, add them to your project's .gitignore file. 将来,将它们添加到项目的.gitignore文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM