Git Cheatsheet
Version control for code
Getting Started
Create a new local repository
$ git init [project name]Clone a repository
$ git clone git_urlClone into a specific directory
$ git clone git_url my_directoryShow file status
Show modified files in working directory, staged for your next commit
$ git statusStage a file
Stages the file, ready for commit
$ git add [file]Stage all changes
Stage all changed files, ready for commit
$ git add .Commit staged files
Commit all staged files to versioned history
$ git commit -m "commit message"Commit tracked files
Commit all your tracked files to versioned history
$ git commit -am "commit message"Discard unstaged changes
Discard changes in working directory which is not staged
$ git restore [file]Unstage a file
Unstage a staged file or file which is staged
$ git restore --staged [file]Set global username
Set the name that will be attached to your commits and tags
$ git config --global user.name "name"Set global email
Set an email address that will be attached to your commits and tags
$ git config --global user.email "email"Enable Git color
Enable some colorization of Git output
$ git config --global color.ui autoEdit global config
Edit the global configuration file in a text editor
$ git config --global --editList local branches
$ git branchList all branches
$ git branch -avSwitch to branch
Switch to my_branch, and update working directory
$ git switch my_branchShow commit log
Show the commit history for the currently active branch
$ git logCompare branches
Show the commits on branchA that are not on branchB
$ git log branchB..branchARemote & Sync
Fetch remote branches
$ git fetch [alias]Merge remote branch (no fast-forward)
$ git merge --no-ff [alias]/[branch]Merge remote branch (fast-forward only)
$ git merge --ff-only [alias]/[branch]Push local commits
$ git push [alias] [branch]Pull changes from remote
$ git pullCherry-pick a commit
Merge a specific commit into your current branch
$ git cherry-pick [commit_id]Add remote alias
$ git remote add [alias] [url]List remotes
$ git remoteList remotes with URLs
$ git remote -vRemove remote
$ git remote rm [remote repo name]Change remote URL
$ git remote set-url origin [git_url]Stash changes
$ git stashList stash stack
$ git stash listApply top stash
$ git stash popDrop top stash
$ git stash dropRemove file from project
$ git rm [file]Example .gitignore file
/logs/*
!logs/.gitkeep
# Ignore Mac system files
.DS_StoreGit Tricks
Rename local branch
$ git branch -m <new_name>Push renamed branch
$ git push origin -u <new_name>Delete remote branch
$ git push origin --delete <old>Rewrite last commit message
$ git commit --amend -m "new message"Search log by term
$ git log -S'<a term in the source>'Log changes to file
$ git log -p <file_name>Graph commit history
$ git log --pretty=oneline --graph --decorate --allList branches with upstreams
$ git branch -vvSwitch to previous branch
$ git checkout -List remote branches
$ git branch -rCheckout file from branch
$ git checkout <branch> -- <file>Git Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status