Skip to main content

Git Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for common Git commands and workflows

Git is a distributed version control system used to track changes in source code. This cheatsheet covers everyday commands for working with repositories, branches, commits, and remote collaboration.

Getting Started

Initialize and configure Git.

CommandDescription
git initInitialize a new repository
git clone urlClone a remote repository
git statusShow working tree status
git config --global user.name "Name"Set global name
git config --global user.email "email"Set global email

Staging and Committing

Add changes and create commits.

CommandDescription
git add fileStage a file
git add .Stage all changes
git commit -m "message"Commit staged changes
git commit --amendAmend last commit
git revert commitRevert a commit

Branches

Create, list, and switch branches.

CommandDescription
git branchList branches
git branch nameCreate a branch
git switch nameSwitch branch
git switch -c nameCreate and switch
git branch -d nameDelete local branch
git branch -D nameForce delete branch
git branch -m new-nameRename current branch

Merge and Rebase

Combine branches and rewrite history.

CommandDescription
git merge branchMerge branch into current
git merge --no-ff branchMerge with merge commit
git merge --abortAbort merge in progress
git rebase branchRebase onto branch
git rebase -i HEAD~3Interactive rebase last 3
git rebase --abortAbort rebase in progress
git cherry-pick commitApply a single commit

Remote Repositories

Work with remotes and synchronization.

CommandDescription
git remote -vList remotes
git remote add origin urlAdd remote
git remote set-url origin urlChange remote URL
git remote remove originRemove remote
git fetchFetch from remote
git pullFetch and merge
git pushPush to remote
git push -u origin branchPush and set upstream
git push origin --delete branchDelete remote branch

Stash

Temporarily save uncommitted changes.

CommandDescription
git stashStash changes
git stash push -m "message"Stash with message
git stash listList stashes
git stash popApply and remove latest
git stash apply stash@{0}Apply without removing
git stash drop stash@{0}Delete a stash
git stash clearDelete all stashes

History and Diff

Inspect history and changes.

CommandDescription
git logView commit history
git log --oneline --graphCompact graph view
git log -p fileHistory of a file
git show commitShow a commit
git blame fileShow who changed each line
git reflogShow reference log (recovery)
git diffUnstaged diff
git diff --stagedStaged diff
git diff branch1..branch2Compare branches

Undo and Cleanup

Discard or reset changes safely.

CommandDescription
git restore fileDiscard local changes
git restore --staged fileUnstage a file
git reset --soft HEAD~1Undo commit, keep changes
git reset --hard HEADReset to last commit
git clean -fdRemove untracked files/dirs

Tags

Create and manage tags.

CommandDescription
git tagList tags
git tag v1.0.0Create tag
git tag -a v1.0.0 -m "msg"Annotated tag
git push --tagsPush all tags
git tag -d v1.0.0Delete local tag
git push origin --delete v1.0.0Delete remote tag

.gitignore

Exclude files from version control.

PatternDescription
*.logIgnore all .log files
node_modules/Ignore directory
!important.logNegate ignore rule
**/buildIgnore in any directory
git rm --cached fileUntrack a tracked file