There are a few different ways to fix your mistakes on git and Github so this is not a comprehensive list. This is simply the list of commands I use regularly.

Understanding Pointers

This is the best explanation of Git EVER.
Source


Re-clone

Command

GIT=$(git rev-parse --show-toplevel)
cd $GIT/..
rm -rf $GIT
git clone https://path/to/repo

Details

❌ Deletes local, non-pushed commits
✅ Reverts changes you made to tracked files
✅ Restores tracked files you deleted
✅ Deletes files/dirs listed in .gitignore (like build files)
✅ Deletes files/dirs that are not tracked and not in .gitignore
😀 You won't forget this approach
😔 Wastes bandwidth


Clean & Reset

Command

git clean --force -d -x
git reset --hard

Details

❌ Deletes local, non-pushed commits
✅ Reverts changes you made to tracked files
✅ Restores tracked files you deleted
✅ Deletes files/dirs listed in .gitignore (like build files)
✅ Deletes files/dirs that are not tracked and not in .gitignore

Extra Details

Use --soft if you want to keep your changes.

git reset --soft HEAD^     

Use --hard if you do not want to keep your changes.

git reset --hard HEAD^

Source


Clean

Command

git clean --force -d -x

Details

❌ Deletes local, non-pushed commits
❌ Reverts changes you made to tracked files
❌ Restores tracked files you deleted
✅ Deletes files/dirs listed in .gitignore (like build files)
✅ Deletes files/dirs that are not tracked and not in .gitignore


Reset

git reset --hard

❌ Deletes local, non-pushed commits
✅ Reverts changes you made to tracked files
✅ Restores tracked files you deleted
❌ Deletes files/dirs listed in .gitignore (like build files)
❌ Deletes files/dirs that are not tracked and not in .gitignore


Destroy for Good

If you mess up, you can always use the reflog to find dropped commits.

git fsck --no-reflogs

Git usually stores deleted commits and branches for 30 days. If you don't want files to last 30 days, then you can force Garbage Collection to clean git by forcing reflog to expire.

git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.pruneExpire=now gc

Source


Recovering a deleted branch

If you delete a branch and you want to recover it, here's how.

This will provide you with a reference of all the places HEAD has pointed.

git reflog

Find the commit you want to recover and get its SHA. Then checkout the branch again.

git branch alias_name 7c2xxCOMMITxSHAxxx

Undo from Github but keep the local files the same

Remove the push from github but leave the local repository intact.

git push -f origin HEAD^:master

Undo a commit and redo

If you accidentally commit, want to undo the commit and keep them unstaged while preserving the files on your disk unchanged, here's how.

Mistakingly commit something.

git commit -m "Something terribly misguided"

Revert the commit and keep the files unstaged.

git reset HEAD~
<< edit files as necessary >>

Make your changes and add the files again.

git add -A

Commit.

git commit -c ORIG_HEAD

Source


Hard Reset to Github

This is one of the most destructive ways of undoing your work. Please be careful with these two commands because there may be no way of going back.

LONGHAND

First review which previous commit you want to revert to.

git log

Paste the hash here

git reset --hard 71c2xxxxxxxxxxxxxxxxxef479

Publish to Github.

git push --force

SHORTHAND

This does the same thing as above. The + means -force.

git push origin +71c2xxxxxxxxxxxxxxxxxef479:master

Renaming Branches

You can rename an existing branch by moving it (-m).

git branch -m existing_name new_name

Simple Script

If you just want a simple bash script to do most of the heavy lifting, try this.

#!/bin/sh
git reset --hard
git clean -f -d
git checkout HEAD

Resources