Wednesday, December 11, 2013

The Git command every "junior" dev needs to save their butt.

Unless your perfect and never accidentally hit the wrong key when you don't mean to than keep reading.

Most Github articles walk you through some good basic commands others are WAY too advanced and in depth for beginners.  This command is the one to know when you have messed up. After knowing this command, you will have the absolute confidence and ability to not freak out when you do make a mistake.


I was doing front end work last week and made the mistake of getting caught up emotionally in an email from one of my bosses to check on a different site I had just made...BLAH..BLAH..BLAH long story short I ended up making some tweaks to the site that I was emailed about and quickly committed the changes as a "dumb-trying-to-impress-his-boss-noob" developer does.

Now here is where the story actually gets good. 3 Months ago I would have been freaking out and cursing myself under my breath for not being more careful and "measuring twice before cutting once".

However now after committing the coding atrocity I simply typed in 1 command and life was good once again and no one was the wiser to the fact that I had violated every good and healthy safety code check that should be done before committing your work. The magic Git command?



All I typed in was:            'git reset HEAD~1'


That's it! That command is your: "Get out of jail, I messed up with Git command"!



The best part is, its a totally safe command that doesn't erase your work or do anything that you don't want to happen. You are simply resetting the last commit, it doesn't erase anything it simply resets everything to how it was before the commit. You can reset the last commit many other ways, but for me this was a really easy one to remember that hopefully I WON'T need anytime soon  =-)

Please share in the comments what I did wrong, I know I was an idiot but being able to fix my mistake and not having to ask for help from the other developers, like the first month at work. I  realized that other young developers might want to keep it handy as well =-)

7 comments:

  1. This is very much appreciated. I have not had to unravel anything to such a degree since I am only working on my own dinky rails project at the moment but I will keep this in mind. Also if you come anymore of these n00b tips, please share. These info really helps really helps.

    ReplyDelete
  2. Thanks Brian, I will keep on sharing if you keep on reading :-)

    ReplyDelete
  3. How to undo the last Git Commit - http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit has been my reference for long; I go looking for it once every 2-3 months :-)

    ReplyDelete
  4. @Prakash Thanks for the comment and link!

    ReplyDelete
  5. Hey Joshua - this article should help too :) http://sethrobertson.github.io/GitFixUm/fixup.html

    p.s. I've been following your blog since I saw the Hacker News post. I seriously went from the very first post when you started to learn Ruby and read all of them in a row. Keep up that drive!

    ReplyDelete
  6. OH. while I'm here, I was wondering whether you'd like to be interviewed for a blog post for Thinkful - an online programming school. My contact info is bhaumik@thinkful.com if you're interested :)

    ReplyDelete
  7. @Bhaumik Sorry just saw this :-( ....would love to!!

    ReplyDelete