I just launched a new course on ES6! Use the code WESBOS for $10 off.

Strengthen your core JavaScript skills and master all that ES6 has to offer. Start Now โ†’

๐Ÿ”ฅ Git Hot Tips

Don’t you just love little tips that make you say “How did I not know that?!”?

Small incremental improvements are the way that you inch towards mastery of a topic. Every day I learn little things about web development that as a whole make me much better at my job.

This is a list of git tips โ€” you may know a few of them but I bet there are a few nuggets that will help you improve your workflow 1, 5 or 10%.

A big thanks to everyone who shared their tips on twitter.

git add -p

Rather than git add everything or individual files, this -p will allow you to step through each change, or hunk, and decide if you’d like to commit it. This is really handy if you have made two different changes to the same file and want to commit them separately.

git log -5 --pretty --oneline

View your last 5 latest commits each on their own line.

๐ŸŸ @luclemo

git shortlog -sn

Quickly get a list of contributors and see how many commits each person has.

๐Ÿ‘ @csswizardry

git log --all --graph --decorate --oneline --simplify-by-decoration

This one is the best – you need to see what it does visually:

You’ll never remember this one so put it in your ~/.gitconfig file under [alias]

wow = log --all --graph --decorate --oneline --simplify-by-decoration

๐Ÿ˜ฎ @innovati and ๐Ÿ™Œ @winstonswchow

git checkout pr/123

Quickly check out a remote for pull request review. You’ll need to set it up like this.

๐Ÿค˜ @linclark

git diff --shortstat "@{0 day ago}"

See how many lines of code you have written today.

๐Ÿ’ƒ @innovati

git checkout -

It’s like the jump button on your TV remote – jump back to to your last branch.

๐Ÿ’† @everyone

git reset --soft HEAD~3

A soft reset will keep your changes but allow you to “uncommit” something.

Instead of doing a squash, Dan prefers to dial back HEAD any number of commits and then add & commit everything into a single commit.

๐Ÿ‡ท๐Ÿ‡บ @dan_abramov

git reflog

David says it best โ€” “Don’t worry, it’s probably saved somewhere”. Git reflog allows you to see every step you have made with git allowing you to retract and reinstate your steps.

๐Ÿ˜ฑ @davidnormo

git stash, then git stash pop

Stash let’s you squirrel away unstaged changes, do some work, and then apply those changes back on.

git stash will stash those changes and put them into your list โ€” I like to think of this as an array of changes. Now to get them back you can git stash apply but what Sam is suggesting here is that you use git stash pop instead which will also bring back your changes, but it removes them from your stash “array”.
๐Ÿ @sambreed

git log -S puppy

Search the commit history for the word puppy and display matching commits.

๐Ÿš€ @chrisrockwell

git latest = for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)’

Allows you to view your latest branchces – this is helpful for when your branch names are based on bug IDs that you have a hard time remembering.

๐Ÿ”ฅ @wesbos

git config --global help.autocorrect -1

Mistype or misspell a git command? Immediately re-run the correct command. You can use -1 to 1000 to wait a full second before the command is re-run.

๐Ÿ”ฅ @wesbos

git commit --amend

Great for squashing staged files into your last commit.

๐Ÿ™ @carlosperez

git cherry-pick [hash]

As long as the commit has been fetched somewhere, you can cherry pick that code in your own branch without having to merge the entire thing.

๐Ÿณ @jonalvarezz

git remote update --prune

Remove local branches that have been deleted from your remote (like GitHub). You can always run git remote prune origin --dry-run to see what will be deleted before going all in.

๐Ÿ‰ @mourawaldson

git rebase -i HEAD~4

Interactive rebase allows you to pick and choose which commits you can pick, squash, reword, edit, or fixup

๐Ÿ˜Ž @Stephn_R


Post yours in the comment below!

This entry was posted in Workflow + Tooling. Bookmark the permalink.

42 Responses to ๐Ÿ”ฅ Git Hot Tips

  1. Salazar says:

    Not really a git trick, but my favorite alias: ” com = !git add -A && git commit -m”. Easily do “git com ‘my changes’ “

  2. @fredrivett says:

    One I use a lot, similar to @dan_abramov’s above, allows simple WIP commits, and popping those commits back off.

    alias gwip=’git commit -m “WIP”‘ # commit my things
    alias gwerp=’git reset HEAD^’ # gimme those things back

    Use this multiple times every day ๐Ÿ‘Œ

  3. Salazar says:

    Oh, also: “git reset HEAD~1 –mixed”. Uncommits from log, but changes are still made in place. I save that as an ‘undo’ alias.

  4. Nikin says:

    git stash save “message”

  5. Kevin Kelly says:

    git stash list -5: check out your last five stash changes in list format.

  6. Aaron says:

    `git poop [branch]`


    Switches to the branch and lets you keep your revisions without conflict

  7. Kevin Kelly says:

    git status -s <- shows files that are new and edited only. Talk about short-form

  8. @adanielyan says:

    git rm –cached

    Removes file from git but keeps it in file system

  9. iqbal hasnan says:

    Reset author

    git commit –amend –reset-author –no-edit

    Remove untracked files

    git clean -xfd

  10. WOW. Thanks Wes. I was always wondering how to commit just a hunk of code via command line. In GUI tools like Tower 2 is easy and very helpful. This one is tip of a day!

  11. Antti Ahti says:

    git fetch –prune – Remove local branches that have been deleted from your remote at the same time when you fetch. No need for a separate command.

    git rebase -i HEAD~3 – Edit, squash (=combine), delete or reorder previous commits.

  12. Eliot Sykes says:

    Ace tips! More at this FAQ site full of searchable git tips: http://firstaidgit.io (also accepts contributions via GitHub repo)

  13. Urban Sanden says:

    Good post!

    See changes in a commit
    git show –color –pretty=format:%b [hash]

    Look for a deleted file with name
    git log –all — **/filename.*

    List deleted files in history with full path
    git log –diff-filter=D –summary | grep delete

  14. รtila Camurรงa says:

    git stash -u

    Include untracked files ๐Ÿ™‚

  15. Ruthan says:

    alias glop=”git log –pretty=format:’%C(yellow)%h|%Cred%ad|%Cblue%an|%Cgreen%d %Creset%s’ –date=short | column -ts ‘|’ | less -r”

  16. Chris says:

    Best decision ever: Switching from command line to real IDE, doing everything with proper UI. Seriously! I cannot understand why so many people still work on the command line when it comes to git.

    • Jonathan says:

      Many people prefer the command line because git makes the most sense on the command line. Even the best IDE is still going to abstract a lot of the inner workings of git, making it harder to understand what is actually happening when using git. Personally, I use the command line because it is easier for me to understand what is happening, and I don’t have a problem with people moving from CLI to IDE. However, if people are “learning” git from the IDE, they most likely don’t understand git, and that is scary to me.

    • aFriend says:

      Well, I guess you can use your IDE on your server.

  17. Pingback: pinboard March 13, 2016 — arghh.net

  18. Great list, a lot of them I already use.

    Here are some more:

    # Non-verbose status
    git status -sb

    # Copies current branch name to clipboard (only OSX)
    git rev-parse –abbrev-ref HEAD | tr -d ‘\n’ | pbcopy (useful if added as git alias, e.g. “git cbn”)

    # Create commit with same message as last commit (useful when you have information about ticket, some global project info, โ€ฆ)
    git commit –reedit-message=HEAD –reset-author

    # Search for branch name (useful when added as git alias, e.g. “git brep {something}”
    git branch -a | grep -i

    # Pick range of commits to cherry pick (useful when added as git alias, e.g. “git cherry-pick-range {first commit} {last commit}”)
    git rebase –onto $(git branch-name) $1^ $2

  19. Raphael says:

    Delete all “merged into master” branches:

    git branch –merged master | grep -v “\master” | xargs -n 1 git branch -d

  20. Kaelig says:

    My favorite command is “gh”. I typically execute it right after I push something to GitHub and I want to open a Pull Request.
    It opens github.com in a browser, directly in the same repository and into the current branch.

    # In .profile or .zshrc:
    # Open current repo in a browser on Github
    function github {
    branch=”$(git rev-parse –abbrev-ref HEAD)”
    url=”$(git config –get remote.origin.url)”
    url=${url/[email protected]:/http://github.com/}

    if [[ $1 =~ “compare” ]]; then action=”compare”
    else action=”tree”; fi

    if [[ $2 != “” ]]; then base=”$2…”
    else base=””; fi


    echo “Opening ${url} $(\open ${url})”

    alias gh=github

  21. I’ve an alias `fixup` that does `git commit –amend -C HEAD`. The result is just like a fixup during rebases, i.e. what’s in the staging gets amended to the last commit keeping its commit message. Very useful when you forget to add a file to a commit (beware that if you’ve already pushed the previous commit, you’ll need to force push)

  22. Pingback: Ypsilanti Safe Space (Amplify Learning) – 107 | This Agile Life

  23. Georgie says:

    One of my favourites is `git stash -p` which lets you stash incrementally (in hunks). Great for when you have updated a large amount of files and want to write a few meaningful commits with one task each, instead of one giant commit.

  24. Chris Missal says:

    Just a quick note: Autocorrect units are measured in deciseconds, not milliseconds. So a value of 1000 would be 100 seconds.

  25. Tim Oxley says:

    git stash -k -u

    -k stashes unstaged
    -u stages untracked

    Use to remove everything that’s not going into the current commit before running your tests.

  26. Ahmad Awais says:

    I have written a simple bash function for fetching the pull request in question. It’s setup is really easy.


  27. Phillipp Rรถll says:

    I never get why nobody uses this or knows about “interactive add”, it is way better than git add -p (because -p is only a sub-program of -i): git add -i

Leave a Reply

Your email address will not be published. Required fields are marked *