Skip to content

Instantly share code, notes, and snippets.

@sand3r
Last active November 22, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sand3r/6845be7b6c2908f84fe0 to your computer and use it in GitHub Desktop.
Save sand3r/6845be7b6c2908f84fe0 to your computer and use it in GitHub Desktop.
GIT Cheat Sheet

Git Cheat Sheet

File Operations

Reset a File

git checkout -- {file}

Reset a File To Origin

git checkout origin/{branch} -- file

Checkout specific version of file

git checkout {revision} {file}

View file on remote/branch

git show {remote|branch}:{file}

Branching, Forking

Manual fork

git clone {remote} git remote rename origin upstream

Create repository to push to, then: git remote add origin {remote_repository_url} git add . git push -u origin master

Checkout specific tag

git checkout tags/{tagNumber}

Delete local branches merged to master

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

List Branches

git branch -a (all branches, local and remote)

git branch -r (only remote branches)

List Branches (not) merged to master

git branch --merged master

git branch --no-merged master

List tags

git tag -l

Remove branch from remote

git push -d origin <branch_name>

git branch -d <branch_name>

Rename Branch (local branches only)

git branch -m {old_branch_name} {new_branch_name}

Review change for commit of one file

git diff {hash} HEAD {file}

View git log for author

git log --author="searchterm"

Reset Branch to another Branch

git push -f origin origin/{branch_name}:{branch_name}

Diff

Diff changes between 2 branches

git diff branch1 branch2

For one file:

git diff branch1 branch2 -- filename

Clear all stashes

git stash clear

Recursively update directories with GIT repositories

find . -type d -name .git -exec sh -c "cd \"{}\"/../ && pwd && git pull" \;

Finding Commits

By Author

git log --author="<name or email>"

By Diff

git log --pickaxe-regex -p --color-words -S "<regexp to search for>"

By Message

git log --grep="<message>"

Stashing

Show contents of stash

git stash show <stash_name>

Stats

Per author

git log --author="<name or email>" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

Ranked per author, for all branches

git shortlog -s -n --all --no-merges

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment