1

I checkout branch from a server and add more files, but I don't notice there is no .gitignore, and I run:

git add ./*
git commit "xx"

There are .class files(and other files) are not my want? now I add .gitignore

#
*.class
bin/
gen/

How I cancel that commit and remove all .class ,bin/ and gen/ ?

BollMose
  • 3,002
  • 4
  • 32
  • 41
  • can't you just delete them and commit? – podshumok Nov 21 '13 at 08:00
  • possible duplicate of [How to undo the last Git commit?](http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit) – devnull Nov 21 '13 at 08:11
  • @podshumok: just deleting and commit would remove the files from your checkout, but not from your repository. (For larger files this will bloat the repository considerably.) – michas Nov 21 '13 at 15:25

2 Answers2

1

You can undo your commit by using

git reset HEAD^

After you fixed your .gitignore add everything by using git add ..

michas
  • 25,361
  • 15
  • 76
  • 121
0

It is probably better to not get in the habit of git add . in the first place. If you are committing, you probably want to be aware of what you are committing rather than just assuming things are what you think they are.

If you do happen to commit things that should not be there though, you can amend your commit with git commit --amend after doing a git reset unwanted_file_addition.txt, for example.

If you have pushed a commit up that contains confidential information you will have some additional work to do.

See this recent GitHub Article released in October, if that has happened. It shows how to take care of that issue.

They also reflect my sentiments on the broader stroke ways of adding files: "If you're working from the command line, avoid the catch-all commands git add . and git commit -a, instead use git add filename and git rm filename to individually stage files."

Files that are used in general for your environment, such as editor swap files, project files, such as .project and operating system files that may pollute your projects should be in your system or account .gitignore file located in either /etc/ or other system wide location, and more specific to your personal tools, in your home folder.

vgoff
  • 10,980
  • 3
  • 38
  • 56