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.

Soft vs Hard Reset

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

git reset --soft HEAD^     

Use --hard if you don't care about keeping the changes you made.

git reset --hard HEAD^


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


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


git commit -c ORIG_HEAD


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.


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


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