Skip to content

Getting Confident with Git Part 1

Polina Soshnin edited this page Dec 23, 2015 · 6 revisions

The tl;dr

  • Use a custom command line prompt to show your current Git branch and changes.
  • Always commit all the time!
  • git stash -u is your panic button. Stash your changes if you're not ready to commit.
  • reflog is Git's internal record log. Try it out, it's pretty cool.
  • The default git log isn't super helpful: make it better with an alias! (see below)

####Git Context In Prompt

  • One of the key features of a Git command line workflow is having the current Git context always displayed as part of the prompt.
  • This context lets you confidently work with your repo, helping you to avoid common mistakes like committing to the wrong branch, forgetting to commit a specific file, etc.
  • Here's one that I use. The instructions in the README get you set up: https://github.com/jimeh/git-aware-prompt.

####Always Be Committing!

  • Once you've made a commit, it's darn near impossible to lose that work without trying to.
  • I've never regretted making a commit, but I have regretted not making a commit. So, err on the side of caution and commit all the time!

####Git Stash for Safety

  • git stash -u is a command that will grab all of the changes in the working directory and store them away. This allows you to focus on something urgent and later revisit the code you stashed away. The u flag makes sure it also stashes new and untracked files.
  • To see what stashes you've stored, use git stash list, and go back to your stash with git stash apply

####The Reflog

  • The reflog is Git's internal record of every explicit change we've made. Any time you commit, reset, branch, merge, or otherwise update what Git is focused on (technically what HEAD is pointing at), Git makes a record of that.
$ git reflog
e58f4ea HEAD@{0}: commit: [fix] Uncomment params to /test route doesn't break
db417e9 HEAD@{1}: commit: [refactor] Fix jshint syntax errors
7995bff HEAD@{2}: checkout: moving from master to SimonDing87-development
e8c0a51 HEAD@{3}: checkout: moving from SimonDing87-development to master

The reflog can be focused down to just a specific branch by adding the branch name as an argument.

$ git reflog show master
e8c0a51 master@{0}: pull: Fast-forward
1331dad master@{1}: commit: [doc] Edit README to test Heroku's automated deployment
92c3c20 master@{2}: commit: [feat] Testing Github automatic deployment to Heroku
a5bc4f8 master@{3}: pull: Fast-forward

From there, you could restore your branch to a previous commit, for instance 1331dad, with:

git reset --hard 1331dad

####Decorated Git Log

The default Git log, viewed by running git log, is useful but not necessarily optimized for day-to-day reviewing of history.

A few examples that make viewing easier:

  • git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  • git log --pretty=format:'%C(yellow)%h%C(reset) - %an [%C(green)%ar%C(reset)] %s'
$ git log --pretty=format:'%C(yellow)%h%C(reset) - %an [%C(green)%ar%C(reset)] %s'
e58f4ea - Polina Soshnin [80 minutes ago] [fix] Uncomment params to /test route doesn't break
db417e9 - Polina Soshnin [2 hours ago] [refactor] Fix jshint syntax errors
7995bff - Polina Soshnin [2 hours ago] [fix] Remove merge conflict annotations
102486c - Polina Soshnin [2 hours ago] [fix] Remove merge conflicts
e7234f9 - Simon Ding [22 hours ago] [feat] Add WOEID's for various locations in comments to be called by twitter api.  (I only added it to my local DB for now)

You can create an alias for it by adding it to your .gitconfig. Here's my .gitconfig as an example.