Skip to content

Instantly share code, notes, and snippets.

@nathakits
Last active December 25, 2018 04:15
Show Gist options
  • Save nathakits/10a8365ef22ae6676a61b0ed4f0674a9 to your computer and use it in GitHub Desktop.
Save nathakits/10a8365ef22ae6676a61b0ed4f0674a9 to your computer and use it in GitHub Desktop.
Git command cheatsheet

Git Alias

Set an alias in git

git config --global alias.<command> "<git command>"

Example

git config --global alias.st "status"
git st

Git branch

Remove deleted remote branches on local machine

git fetch --prune

Delete all local branches not on remote

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Rename local branch

git branch -m <new name>

Reset master to origin master

git checkout -B master origin/master

Git LFS

Pull a single file

You can specify the --include or -I flag (they are aliases of each other) to only include a specific filename in your pull. For example, if you only wanted to pull the file called "a.dat", try:

git lfs pull --include "a.dat"

Or, if you only wanted to pull files matching the ".dat" extension, try:

git lfs pull --include "*.dat"

Git Diff

Autocomplete for git diff

Using wildcard to match the file path

git diff -- **/<file path>

Git Log

Revision ranges

git log master                      # branch
git log origin/master               # branch, remote
git log v1.0.0                      # tag

git log master develop

git log v2.0..master                # reachable from *master* but not *v2.0*
git log v2.0...master               # reachable from *master* and *v2.0*, but not both

Basic filters

git log
  -n, --max-count=2
      --skip=2

      --since="1 week ago"
      --until="yesterday"

      --author="Rico"
      --committer="Rico"

Search

git log
      --grep="Merge pull request"   # in commit messages
      -S"console.log"               # in code
      -G"foo.*"                     # in code (regex)

      --invert-grep
      --all-match                   # AND in multi --grep

Limiting


git log
      --merges
      --no-merges

      --first-parent                # no stuff from merged branches

      --branches="feature/*"
      --tags="v*"
      --remotes="origin"
      
## Simplification

git log 
      -- app/file.rb                # only file
      --simplify-by-decoration      # tags and branches

Ordering

      --date-order
      --author-date-order
      --topo-order                  # "smart" ordering
      --reverse

Formatting

      --pretty="..."                # see man git-log / PRETTY FORMATS
      --abbrev-commit
      --oneline
      --graph

Examples

git log --graph --oneline --abbrev-commit
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

Git Reset

Revert a specific file to that commit

git reset <commit hash> <file path>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment