简体   繁体   中英

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. 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. Then you can add the .gem to .gitignore (or *.gem if you don't ever want them) and do a new commit.

This has bitten me enough that I generally avoid git commit -a or git add ing an entire directory. git add -u comes in real handy to add changes only in known files.

Reset HEAD and Commit

First, do a soft reset of your HEAD to the previous commit. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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