Last active
February 19, 2024 06:11
-
-
Save nicokosi/11395075 to your computer and use it in GitHub Desktop.
My own git cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### BRANCHES | |
git checkout -b <branch name> # Create a branch | |
git branch -r # List remote branches | |
git checkout <branch name> # Switch to branch (deprecated) | |
git switch <branch name> # Switch to branch | |
git checkout <commit ID> # Locally retrieve an old revision | |
git fetch # retrieve remote changes (commits) without changing local repo ; can be used to fix sync issue | |
git branch -D branchName # delete local branch | |
git branch -m <newname> # rename current local branch | |
git push origin --delete <branchName> # delete remote branch | |
git push origin :branchName # delete remote branch | |
git push origin <commit>:<branchName> # push up to a commit on a given branch | |
git branch --remote --contains=SHA-1 # List all remote branches that have a given commit (includes merges) | |
git branch -d `git branch --merged | grep -v '^*' | grep -v 'master' | tr -d '\n'` # Remove all local branches that have been merged on branch 'master' | |
git branch --remote --merged # List all remote branches that have been merged in to current branch | |
### HISTORY | |
git log # current (local) branch | |
git log --oneline # current local branche with short format (commit ID + message) | |
git log origin/master # remote branch (example: origin/master) | |
git log -G foo # list commits that have added/deleted the text "foo" | |
git log --perl-regexp --author='^((?!Alice).*)$' # list commits except those from Alice | |
git log --all -S 'regex' # full-text search in all branches! | |
git log branch1..branch2 # list all commits in branch2 that are not in branch1 | |
### LOCAL CHANGES | |
git diff # diff between unstaged changes and remote | |
git diff --cached # diff between staged changes and remote | |
git clean -df # remove local files that are not under version control | |
git reset --hard # discard local commits | |
### STACK FOR LOCAL CHANGES | |
git stash # Push local changes in local stack | |
git stash save "my message" # same with a optional message | |
git stash pop # Pop (last) local changes from local stack | |
git stash pop "my message" # Pop changes by message | |
git stash list # list local stack | |
git stash show # show last changes (file list) from stack | |
git stash show -C # show detailed changes (file content) | |
git stash show stash@{1} # show previous changes (file list) from stack | |
git stash show "my message" # show changes (file list) by message | |
# Clean-up | |
git fetch -p # Remove local branches that have been deleted remotely: | |
## REWRITING HISTORY (WARNING!) | |
git rebase --interactive HEAD~2 # change history for the last 3 commits | |
git rebase --interactive origin/MYBRANCH # change history for all local commits that are not in remote branch MYBRANCH | |
git push --force # May the force be with you! | |
git push --force-with-lease # Might the force be with you?! | |
### Use shortnames (https://git-scm.com/docs/gitrevisions#_specifying_revisions and https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_git_reflog) | |
git diff @{2} # Compare code with the penultimate commit | |
git diff @{push} # Compare code with the remote branch | |
git diff @{u} # Compare code with the remote-tracking branch | |
git diff master@{yesterday} # Compare code with the (latest?) commit from yesterday | |
git diff master@{4.days.ago} # Compare code with the (latest?) commit from 4 days ago | |
git diff HEAD^ # Compare code with its parent commit (the previous one in the commit tree) | |
git diff HEAD^2 # Compare code with its second parent commit ; useful for merge commits | |
git diff HEAD~2 # Compare code with with its grand-parent commit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment