Itération 122442
Itération 122442

Reputation: 2962

Git, cancel latest commit that is first commit

I created the first commit of my project, but forgot to ignore node_modules.

I tried this solution to cancel the commit. However, it returns me this (sorry, virtual machine without copy/paste capacity):

enter image description here

As you can see in the screenshot above, it is not capable of doing what I expected. (red lines to isolate commands and help reading)

How can I cancel latest commit considering that it is the very first commit ?

Upvotes: 3

Views: 150

Answers (2)

torek
torek

Reputation: 487745

The reason your git reset command fails is that git reset does not actually remove a commit. Instead, it moves you from the current commit, as identified by your current branch name, to the previous commit on that same branch. But there is only one commit! It has no previous commit. There is therefore nowhere else to go.

As Maroun said, you can update your index—in this case, by running git rm --cached node_modules to remove all node_modules files from the index, while leaving them alone in your work-tree—and then run git commit --amend. What --amend does is modify the commit operation. Instead of adding a commit to the current branch, it shoves the current commit out of the way, so that the new commit takes its place.

When you have more than one commit, this makes more sense pictorially. For instance, if you had three commits—with three different hash IDs that we'll just call A, B, and C here—you could draw them like this:

A--B--C   <-- master

Using git commit --amend shoves C out of the way when creating D:

     C  [abandoned]
    /
A--B--D   <-- master

Note that existing commit C remains in your repository. There's just no obvious way to find it any more: git log won't show it, because git log now starts with D, which is now the last commit on your master, and works its way backwards to B and then to A.

In this case, though, there are no previous commits. Your existing set of commits would be drawn more as:

A   <-- master

When Git shoves A out of the way to put B in, you get:

A

B   <-- master

Again, there's no obvious way to find the commit you've just "replaced". It does still exist, and will stick around for at least 30 days by default in case you decide you want it back, but for most purposes, it's just gone. Eventually, your Git will drop it, because it has no name by which anyone can find it.

However, if you've sent commit A to some other Git, using git push origin master for instance, that Git still has a copy of commit A. You'll have to get them to replace their A with your new-and-improved B. To do that, in general you will need to use git push --force origin master or git push --force-with-lease origin master.

If you have not sent commit A to some other Git repository, none of this is necessary.

Upvotes: 1

Maroun
Maroun

Reputation: 95948

You don't need to remove the commit. You can remove the node modules, edit the .gitignore and amend the changes to the last commit (which is also the first in your case):

# changes
git commit --amend
git push -f

If you insist to remove the commit, you can run

git update-ref -d HEAD
git push -f

Upvotes: 3

Related Questions