Git CheatSheet
Learn GIT: https://learngitbranching.js.org/
Contents:
Cherry-pick
# pick specified commit git cherry-pick 3ea6207fe84
Stash

# Stash modified/staged files git stash # Stash modified/staged/untracked files git stash -u # Stash modified/staged/untracked/ignored files git stash -a # Stash modified/staged files with message git stash save "stash message" # Show all available stashes git stash list # Get stash to working gir & delete the stash (last or specified) git stash pop git stash pop stash@{1} # Get stash to working gir, but not delete the stash (last or specified) git stash apply git stash apply stash@{1} # Recover dropped stash (you need to know it's hash) git stash apply [stash-hash] # Delete stash (last or specified) git stash drop git stash drop stash@{1} # Show stash diff (last or specified) git stash show git stash show stash@{1} # Show all details (the patch) git stash show -p stash@{1} # Create branch from stash & delete stash (last or specified) git stash branch [branch-name] git stash branch [branch-name] stash@{1}
By default git SKIP untracked or ignored files for stash.
Remote, Fetch, Pull, Push
Retrieving updates from another repository and updating local repos
# add a git URL as an alias git remote add [alias] [URL] # Fetch down all branches from remote repo git fetch [alias]
Pull
# Fetch and merge any commits from the tracking remote branch git pull # Pull only if it can be “fast-forwarded” (without creating new commits) git pull --ff-only
Push
# Push local branch commits to remote branch git push [alias] [branch] # Force push (with changing history) git push [alias] [branch] --forse = git push [alias] +[branch]
Merge, Rebase
Merge
# merge the specified branch’s history into the current one git merge [branch] # merge the specified branch’s history into the current one # fail if linear history fails git merge [branch] --ff-only # merge a remote branch into your current branch to bring it up to date git merge [alias]/[branch]
Rebase
# apply any commits of current branch ahead of specified one git rebase [branch] # interactive rebase (using commit hash) git rebase -i 972078a # interactive rebase (using offset) git rebase -i HEAD~3
Doc: https://git-scm.com/docs/git-rebase
Checkout, Branch
Checkout
# Switch to another branch and check it out into your working directory git checkout [branch-name] # Create branch on your local machine and checkout to it: git checkout -b [new-branch-name]
Branch
List (show):
# Show all local branches git branch # Show all local branches & last commit message git branch -v # Show branches & related remote branch (upstream) & last commit git branch -vv # Show all remote branches git branch -r git branch --remotes # Show local & remote branches git branch --all # List branches available for checkout git branch -v -a # Show of branches that already was merged with current git branch --merged # Show of branches that not merged with current git branch --no-merged
# Create a new branch at the current commit git branch [branch-name] # Rename branch git branch --move [old-branch-name] [new-branch-name] # Relate remote and current branches git branch --set-upstream-to=origin/[branch-name] git branch -u origin/[branch-name] # Push and Relate remote and current branches git push --set-upstream origin [branch-name] git push -u origin [branch-name]
Delete (remove):
# Delete local branch only if it was fully merged git branch -d [branch-name] # Force delete local branch git branch -D [branch-name] # Delete branch from remote repo git push origin --delete [branch-name] # Or a short variant git push origin :old_branch
Switch
# Create a local branch from the fetched remote branch (Git 2.23+) git switch [branch-name] # In this case Git is guessing that you are trying to checkout # and track the remote branch with the same name. # This can be disabled with --no-guess git switch [branch-name] --no-guess # -c to create a new local branch git switch -c [local-branch-name] origin/[remote-branch-name]
Add, Commit, Status, Log
Commit
# commit your staged content as a new commit snapshot git commit -m "your commit message" # add & commit git commit -am "your commit message" # Append current changes to last commit git commit --amend # Create empty commit git commit –allow-empty -m 'create branch'
Status (diff show)
# show modified files in working directory, staged for your next commit git status # diff of what is changed but not staged git diff # diff of what is staged but not yet commited git diff --staged # show the diff of what is in branchA that is not in branchB git diff branchB...branchA # show any object in Git in human-readable format git show [SHA]
log
# show all commits in the current branch’s history git log # show the commits on branchA that are not on branchB git log branchB..branchA # show the commits that changed file, even across renames git log --follow [file] # Show compact git log --oneline # Show tree git log --graph git log --graph --oneline
add
# Stage all repo files (not from .gitignore) git add . # Stage all changes in <directory> for the next commit. git add [directory] # Stage all changes in <file> for the next commit. git add [file]
Tagging
# list all tags git tag git tag -l git tag --list # see tag data along with the commit that was tagged git show v1.4 # search for tags that match a particular pattern git tag -l "v1.8.5*" # create a simple tag (holds only commit) git tag v1.4 # create an annotated tag (holds commit and tag data) git tag -a v1.4 -m "Version 1.4" # create a tag later for commit 9fceb02 git tag -a v1.2 9fceb02 # push tag git push origin v1.5 # push all tags git push origin --tags git push --tags # delete tag (local only) git tag -d v1.4 # delete tag remotely git push origin --delete v1.4 # checkout the tag commit git checkout v1.4
clone, init (create repo)
# Create Git repo in current folder git init git init <project name> # Create remote repo in current folder by URL git clone <URL> # Copy remote repo in specified folder by URL git clone <URL> <folder> # Copy remote repo with only specified branches git clone -b <branch_name> --single-branch <URL> # Copy remote repo with only specified branches in specified folder git clone -b <branch_name> --single-branch <URL> <folder> # Copy remote repo with 5 commits last only (limit history) git clone <URL> --depth=5 <folder>
Config (settings, setup, option)
remote
# view remote URLs git remote -v # add remote repo (takes two arguments) git remote add origin [email protected]:USERNAME/REPOSITORY.git # change a remote repository's URL (takes two arguments) git remote set-url origin [email protected]:USERNAME/REPOSITORY.git # rename a remote repository (takes two arguments) git remote rename origin my_name
More: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories
list
# Show all settings git config --list # Or see files: `~/.gitconfig` (user wide) `.git/config` (repo wide) # Show global options only git config --global --list # Show system options only git config --system --list # Show options and files where they defined (global, user, repo, etc...) git config --list --show-origin
Get
# Show option value git config user.name # or git config --get user.name # Show option value and file where it places git config --show-origin user.name # Show option value from specified scope git config --system user.name
Set
# Set exact setting git config --global user.name "[Kama]" git config --global user.email "[[email protected]]" # Set automatic command line coloring for Git for easy reviewing git config --global color.ui auto # Set `core.autocrlf=true` in global scope git config --global core.autocrlf true # Set system wide .gitignore patern for all repos git config --global core.excludesfile [file]
Remove / Unset
# Remove option from repo scope git config --unset user.name # Remove option from global scope git config --global --unset user.name
Tracking (rm mv)
# delete the file from project and stage the removal for commit git rm [file] # Remove single file from git stage git rm --cached www/.env # change an existing file path and stage the move git mv [existing-path] [new-path]
Revert
# Creates new commit in which all changes of another commit will be reverted git revert 0ad5a7a6 # Revert by modifying your working tree and index without creating a commit git revert -n 0ad5a7a6 git revert --no-commit 0ad5a7a6 # The target commit is 2 commits behind HEAD on the current branch git revert HEAD~2 # Revert a set of Git commits, between two revisions. # The older commit should come first, followed by the newer commit. # Revert these two commits, plus every commit in between them. git revert HEAD~5..HEAD~2 # Revert merge and leave only 1 parent git revert -m 1 HEAD git revert --mainline 1 HEAD
Reset

git reset = git reset --mixed HEAD git reset HEAD git reset [hash] # reset all: commit history, stage snapshot, working dir git reset --hard [hash] # reset: commit history, stage snapshot git reset --mixed [hash] # reset: commit history git reset --soft [hash] # unstage a file, but retaining the changes in working dir git reset [file]
Grep (serach)
# Search "search_query" in working dir git grep -n search_query # Find commits where ZLIB_BUF_MAX exists git log -S ZLIB_BUF_MAX --oneline # Search with Regex git log -G regex --oneline
-
See: https://education.github.com/git-cheat-sheet-education.pdf