0

I'm not supposed to commit to dev branch directly, but I accidentally committed something but hasn't pushed it yet, what should I do if I want to cancel it without affecting anything else?

I found a command online: git reset --hard HEAD^ can I use it?

wawawa
  • 2,835
  • 6
  • 44
  • 105
  • 3
    You'll lose your local changes. Rather create a backup branch from you local dev branch like `git checkout -b backup` then reset it. After that you can raise PR from backup branch to dev. – Rajender Joshi Jul 02 '21 at 11:33
  • 1
    As a general tip - if you're not sure what you're doing with git - backup your repository folder before doing anything risky. Hard reset would get rid of the changes you made in that commit and it would be slightly difficult to get them back if you wish too, you may want to do a `--mixed` reset instead to maintain the changes in your working tree. – Omer Tuchfeld Jul 02 '21 at 11:33
  • 1
    Does this answer your question? [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – Thomas Jul 02 '21 at 11:34
  • This is already answered [here](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – clamentjohn Jul 02 '21 at 11:34

1 Answers1

1

You can just use it without --hard flag

git reset HEAD~1
Parth Savaliya
  • 353
  • 6
  • 13