DEV Community

Pheak Pheasa
Pheak Pheasa

Posted on

2 Ways to save your life when you confused push any code or file to your branch.

Way 1

1. Find your committed hash by :

git log
Enter fullscreen mode Exit fullscreen mode

2. After found your commit hash you need revert it by :

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

3. choose one at bellow with responsibility:

🟢 Uncommit last commit (keep files):

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

✅ Files stay staged, just remove commit.

🟡 Uncommit & Unstage (keep changes in working directory):

git reset --mixed HEAD~1
Enter fullscreen mode Exit fullscreen mode

✅ Files stay, but unstaged (not in commit).

🔴 Uncommit & Remove changes:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

⚠️ Files & commit both gone! Careful!

🟣 Uncommit pushed commit (force push):

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
git push --force
Enter fullscreen mode Exit fullscreen mode

✅ Removes commit on remote branch too.

Way 2 in next post.

Top comments (0)