On GitHub, I forked a repositary and cloned on my PC. Then, for testing purpose, I edited a file, made a commit and pushed it to GitHub. But now I would like to completely delete this commit.
I did the following:
git reset --hard <sha1_of_previous_commit>
git push --force
It looked OK, but my commit was still accessible on GitHub by URL with SHA1 of my commit. So I deleted the repositary on GitHub, then the URL was saying Not Found.
But if I fork the same repositary again, my commit is again accessible by this URL. Please help me, how the hell I can get rid of this commit?
If the . git size did not decrease, check if there are tags or anything referencing the offending commit. You will have to delete any tags referencing the commits from the origin too. For more complicated situations, you can try the answer by AD7six, but this is just a simple and clean way to do it.
First, remove the commit on your local repository. You can do this using git rebase -i . For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up.
Delete Files From Git Repository only To achieve that, we simply execute the “git rm” command with the “–cached” option. As you can see, after executing the “git rm” command, the file is still on the filesystem. Awesome, you simply have to commit and push the changes now for them to be effective.
The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.
This has worked for me:
I have reset the head to the number of commits back by 2
git reset --hard HEAD~2
and force pushed my branch to remote by
git push -f origin my_branch_name
I could not see any traces of the commits on remote repo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With