JS

Git tricks

John Sylvain - February 03, 2019

Here's a (growing) collection of git commands/tricks that I find useful. While there are many more, these are ones that I use on a daily basis.

General

  1. git checkout -: switch to the previous branch. Very similar to cd -

  2. git checkout .: discard all unstaged changes. This action is irreversible.

  3. git add -p: Interactively stage files. Similar experience to git rebase -i

  4. gitignore <language>: This is a custom function you can add to your bashrc. Please refer to gitignore.io for more usage information.

    gitignore() {
      curl -L -s "https://www.gitignore.io/api/$1" >> .gitignore
    }

Handling mistakes

  1. git reset HEAD~1: "un-commit" your last commit

  2. git branch -m <original> <target>: move branch to new location. Useful if you misspell a branch name. Similar to mv. If you have already pushed the bad branch, run git push origin --delete <original> then git push origin <target>

  3. git commit --amend: add changes to the previous commit. If changes have already been published, use git push -f to force push the changes to the remote origin.

Logging

  1. git log --oneline: Single line git log.

  2. git log --graph: log git history in graph form.

Addons