1

I created a repository, however I accidentally committed files that I do not want to commit at all. I'd like to cancel that first commit. git reset HEAD~ doesn't work for me, I get an error:

$ git reset --soft HEAD^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I tried to do "reverse commit" in Attlasian SourceTree. That just removed all files, but I don't want to delete them, so I reverted it again. I now have even more commits which I want to remove.

image description

I'd like to cancel all commits in this repository, then add .gitignore first, then add other files. The commits are local only, I did not push anything. Some of the files in the first commits must not be ever pushed online, since they contain classified data.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

1 Answers1

1

Delete the repository, start another.

Since the repo contains "classified data", nuking the site from orbit seems best.

Git repos are cheap and your local copy is entirely contained in the .git directory. If you screw up the first commit, delete the .git directory (keep your other files) and git init a fresh one.

Delete the online repository as well and create a new one. A new online repository should have instructions to connect with your existing local repository; git remote add origin and such.

Use git reset & git amend.

An approach more generally applicable is to use git reset. This allows you to arbitrarily move branch heads around. git reset --hard origin/master will return master to your first commit (origin/master happens to point at the first commit, if not you'd use its commit id) and throw out the later ones. --hard says to reset both the staging area and the working copy (ie. the files on disk).

From there you can use git commit --amend to change the previous (ie. first) commit. Instead of making a new commit, this will alter the previous one. This is handy for making changes, or even just fixing a typo in the commit message.

It looks like you did already push your first commit. Since you're altering that commit, you won't be able to git push. You'll have to git push --force to overwrite what's in the remote repository. It sounds like that's what you want.

Schwern
  • 153,029
  • 25
  • 195
  • 336