Recover from a GIT mistake

Maybe one day in the future we could use technology to put our lives under version control and return back to the good old days.

One of the great things about GIT. Is that you cannot really make a mistake or in other words any mistake you make is recoverable.

Since most of us are humans we will often make mistakes in life and when using GIT. Unlike life, GIT is a good version control system that makes it possible to recover from mistakes.

Any way here are a few useful commands to help you recover from some common mistakes

How to undo your last commit

git reset -- soft HEAD^ 

Un-stage staged files

This takes everything in the staging area out of the staging area

git reset HEAD 

If you want to unstage a specific file you can do this

 git reset HEAD filename_of_file_to_unstage.txt

Remove changes made to a modified file

This one is useful if you have made a lot of changes exited your text editor realised you should not have made those changes and CTRL-Z (undo) is no longer possible

git checkout -- filename_of_file_to_unmodify.txt

Need more help

If your mistakes go deeper than one-commit-ago, are overwritten, on deleted branches or something even more fancy and complicated (maybe due to a workflow put together by an advanced alien race) I suggest looking into the --amend option that goes with the commit command


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s