Skip to content

Instantly share code, notes, and snippets.

@babakhani
Last active January 7, 2021 21:04
Show Gist options
  • Save babakhani/691fe178360ae77b1cf00b750c30d139 to your computer and use it in GitHub Desktop.
Save babakhani/691fe178360ae77b1cf00b750c30d139 to your computer and use it in GitHub Desktop.
# Useful Git Commands
## oficial git cheet-sheat
https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
https://github.com/git-tips/tips#everyday-git-in-twenty-commands-or-so
https://github.com/dictcp/awesome-git
#### Pretty Log
`git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative`
#### Search commits for a string or code
`git log -G "<search-string>" --pretty=format:"%C(yellow)%h %Creset%s %Cgreen(%cr) %Cblue[%cn - %ce]" --decorate`
#### List all the conflicted files
`git diff --name-only --diff-filter=U`
`git diff --check`
#### Search change by content / date
`git log --no-merges --raw --since='2 weeks ago'`
`git whatchanged --since='2 weeks ago'`
#### Cleanup remote-removed branch
See: [stackoverflow](https://stackoverflow.com/questions/16590160/remove-branches-not-on-remote/16590499#16590499)
`git branch -vv | grep ': gone]'| grep -v "\*" | awk '{ print $1; }' | xargs git branch -d`
#### Clone a shallow copy of a repository
`git clone https://github.com/user/repo.git --depth 1`
#### Marks your commit as a fix of a previous commit.
`git commit --fixup <SHA-1>`
#### Status of ignored files.
`git status --ignored`
#### Skip staging area during commit.
`git commit --only <file_path>`
#### Commits in Branch1 that are not in Branch2
`git log Branch1 ^Branch2`
`git log -S'<a term in the source>'`
`git log --all --grep='<given-text>'`
`git log --since='FEB 1 2017' --until='FEB 14 2017'`
`git log --perl-regexp --author='^((?!excluded-author-regex).*)`
#### Group commits by authors and title
`git shortlog`
#### See all commits made since forking from master
`git log --no-merges --stat --reverse master..`
#### List unpushed git commits
`git log --branches --not --remotes`
`git log @{u}..`
`git cherry -v`
#### Show all local branches ordered by recent commits
`git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/`
#### Show the author, time and last revision made to each line of a given file
`git blame <file-name>`
#### Show how many lines does an author contribute
`git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | gawk '{ add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc += <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf "added lines: %s removed lines: %s total lines: %s
", add, subs, loc }' -`
`git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | awk '{ add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc += <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf "added lines: %s, removed lines: %s, total lines: %s
", add, subs, loc }' - # on Mac OSX`
#### Changes staged for commit
`git diff --cached`
`git diff --staged`
#### List all branches that are already merged into master
`git branch --merged master`
#### Quickly switch to the previous branch
`git checkout -`
`git checkout @{-1}`
#### Checkout a new branch without any history
`git checkout --orphan <branch_name>`
#### Restore file to a specific commit-hash
`git checkout <commit-ish> -- <file_path>`
#### Extract file from another branch.
`git show <branch_name>:<file_name>`
#### Restore deleted file.
`git checkout <deleting_commit>^ -- <file_path>`
#### Remove branches that have already been merged with master
`git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d`
`git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d # will not delete master if master is not checked out`
#### Delete remote/local branch
`git branch -d <local_branchname>`
`git push origin --delete <remote_branchname>`
#### Delete remote/local tag
`git tag -d <tag-name>`
`git push origin :refs/tags/<tag-name>`
#### Undo local changes with the last content in head
`git checkout -- <file_name>`
#### Revert: Undo a commit by creating a new commit
`git revert <commit-ish>`
#### Reword the previous commit message
`git commit -v --amend`
#### See commit history for just the current branch ???
`git cherry -v master`
#### Amend author.
`git commit --amend --author='Author Name <email@address.com>'`
#### Changing a remote's URL
`git remote set-url origin <URL>`
#### Get list of all local and remote branches
`git branch -r`
`git branch -a`
#### Stage parts of a changed file, instead of the entire file ???
`git add -p`
#### Pick commits across branches using cherry-pick ???
`git checkout <branch-name> && git cherry-pick <commit-ish>`
#### Find out branches containing commit-hash
`git branch -a --contains <commit-ish>`
`git branch --contains <commit-ish>`
#### Git Aliases
`git config --global alias.<handle> <command> `
`git config --global alias.st status`
#### Stash
`git stash list`
`git stash apply <stash@{n}>`
`git stash -u`
`git stash -k ??`
`git stash pop`
`git stash apply stash@{0} && git stash drop stash@{0}`
`git stash clear`
`git stash drop <stash@{n}>`
#### Grab a single file from a stash
`git checkout <stash@{n}> -- <file_path>`
`git checkout stash@{0} -- <file_path>'`
#### Saving current state with message
`git stash save <message>`
#### Show all tracked files
`git ls-files -t`
#### Show all ignored files
`git ls-files --others -i --exclude-standard`
#### Create new working tree from a repository (git 2.5)
`git worktree add -b <branch-name> <path> <start-point>`
#### Create new working tree from HEAD state
`git worktree add --detach <path> HEAD`
#### Untrack files without deleting
`git rm --cached <file_path>`
#### Number of commits in a branch
`git rev-list --count <branch-name>`
#### Backup untracked files.
`git ls-files --others -i --exclude-standard | xargs zip untracked.zip`
#### List of all files changed in a commit
`git diff-tree --no-commit-id --name-only -r <commit-ish>`
#### Auto correct typos.
`git config --global help.autocorrect 1`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment