Show Menu
Cheatography

Git Commands Cheat Sheet by

List of frequently used git commands

Main Commands

git init
Create git repository here (decen­tra­lized)
git init --bare
Create bare git repository (centr­alized)
git clone repo
Clone repository at address repo
git clone -b branch repo
Clone branch from repository

Status

git status
List changes
git status --short --branch
List changes (compact)
git log
Display commit history
git log --oneline --decorate --graph --all
Display visual commit history
git config --global core.a­utocrlf true
Ignore OS-spe­cific line endings

Commits

git add hello.py
Add hello.py (or stage changes) for next commit
git rm --cached hello.py
Remove hello.py from git tracking
git checkout -- hello.py
Discard all local changes to hello.py
git add -u
Stage all changes under tracking
git add -A
Stage all changes (new files and deletions included)
git update­-index --assu­me-­unc­hanged hello.py
Ignore changes to hello.py locally
git add --inte­ractive
Review and stage changes
git commit
Create a commit (snapshot)
git commit -m "­me­ssa­ge­"
Create a commit with message
git commit -a
Commit every change under tracking
git commit -ammend
Combine current and last commit (corre­ction)
git diff
Show all unstaged differ­ences
git diff --cached
Show all differ­ences including staged ones
git diff branc­h1..­bra­nch2
See differ­ences between branch1 and branch2
You can use commits instead of branch names. HEAD refers to current point in git.

Reseting

git reset --soft commit
Stage all changes since commit (used for squashing)
git reset --hard
Discard all local changes and reset to last commit

Mainte­nance

git revert commit
Creates a new commit out of an old commit (turn back in history)
git clean -dfx
Delete all untracked files and directories
Dangerous
git clean -dfxn
Dry run - Show which files will be deleted
git gc --aggr­essive --prun­e=now
Reduce file size of repository by pruning
git checkout file
Discard local changes to file
bfg -delet­e-files file
BFG cleans sensitive data file
 

Remote Management

git remote add target repo
Add remote target at address repo
git remote -v
List all remotes
git remote set-url target repo
Changes the address of remote target
git push -f origin commi­tma­ster
Forces origi­n/m­aster branch to be moved to commit
git pull (--rebase) target branch
Push existing commit to targe­t/b­ranch
git push target branch
Push branch to targe­t/b­ranch
git push target branc­h1:­bra­nch2
Push local branch1 to targe­t/b­ranch2
git push -u target branch
Push and track targe­t/b­ranch. It sets upstream for default pull / push
git push target --delete branch
Deletes targe­t/b­ranch at remote
git fetch target
Update branch inform­ation of target remote
Note: repo might start with git@ or https. When it starts with git@ an SSH key is needed for pull/push operat­ions.

Branch management

git checkout target
Switch to target branch
git checkout -b feature
Create new branch from current commit
git checkout -b feature master
Create new branch feature from master branch
git branch -a
List all branches
git branch -r --merged master
List remote branches that are merged with master
git merge feature
Merge feature branch into current branch
git merge --no-ff feature -m "­me­ssa­ge­"
Merge feature with a commit (no fast-f­orward)
git branch -d feature
Delete feature branch (if already merged)
git branch -D feature
Force delete feature branch
Dangerous
git branch -f feature commit
Move branch head feature to commit
Dangerous
Use
git for-ea­ch-ref --sort­=co­mmi­tte­rdate refs/h­eads/ --form­at=­'%(­HEAD) %(refn­ame­:short) - %(obje­ctn­ame­:short) - %(cont­ent­s:s­ubject) - %(auth­orname) (%(com­mit­ter­dat­e:r­ela­tiv­e))'
to print summary of each branch, last commit, and other info

Tag management

git tag -a v1.0.0 -m "­me­ssa­ge­"
Creates an annotated tag with message
git push target --tags
Push local tags to remote target
git tag -d v1.0.0
Delete local tag *v1.0.0­"
git push target :refs/­tag­s/­v1.0.0
Delete remote tag targe­t/v­1.0.0

Semantic Versioning

git describe --tags --dirty
Describe current commit using tags

Submodules

git submodule update --init --recu­rsive .
Pull all submodules (linked reposi­tories)
git submodule status folder
Show the status of submodule under folder
           

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Git-svn for Beginners Cheat Sheet
          Top 30 linux shell tricks Cheat Sheet