← Back to Home

Git Cheat Sheet

Essential Git commands for version control

git init
Initialize a new Git repository in the current directory. Creates a .git folder with repository metadata.

git init

Keywords: initialize, repository, new, create

git clone
Clone a remote repository to your local machine. Downloads all files, branches, and commit history.

git clone https://github.com/user/repo.git

Keywords: clone, download, remote, copy

git add
Stage changes for commit. Use . to add all changes, or specify individual files. Prepares changes for the next commit.

git add . && git add file.txt

Keywords: stage, add, changes, index

git commit
Create a commit with staged changes. Use -m for inline message, -a to stage and commit all changes, --amend to modify last commit.

git commit -m "Add new feature"

Keywords: commit, save, message, snapshot

git push
Upload local commits to remote repository. Use -u to set upstream branch, --force to overwrite remote history.

git push origin main

Keywords: push, upload, remote, publish

git pull
Download and merge changes from remote repository. Combines git fetch and git merge in one command.

git pull origin main

Keywords: pull, download, merge, sync

git status
Show working directory status. Displays staged, unstaged, and untracked files. Essential for understanding current state.

git status

Keywords: status, changes, staged, untracked

git log
Display commit history. Use --oneline for compact view, --graph for visual branch structure, -n to limit results.

git log --oneline --graph -10

Keywords: log, history, commits, graph

git branch
List, create, or delete branches. Use -a to show all branches, -d to delete, -m to rename current branch.

git branch feature-branch

Keywords: branch, create, list, delete

git checkout
Switch branches or restore files. Use -b to create and switch to new branch, -- to restore specific files.

git checkout -b new-feature

Keywords: checkout, switch, branch, restore

git merge
Merge another branch into current branch. Creates merge commit unless fast-forward is possible.

git merge feature-branch

Keywords: merge, combine, branches, integrate

git rebase
Reapply commits on top of another branch. Use -i for interactive rebase to edit commit history.

git rebase main

Keywords: rebase, replay, history, interactive

git stash
Temporarily save uncommitted changes. Use pop to restore, list to view stashes, drop to delete.

git stash && git stash pop

Keywords: stash, temporary, save, restore

git reset
Reset current branch to specific commit. Use --soft to keep changes staged, --hard to discard all changes.

git reset --hard HEAD~1

Keywords: reset, undo, commit, history

git diff
Show differences between commits, branches, or working directory. Use --staged to see staged changes.

git diff HEAD~1..HEAD

Keywords: diff, differences, changes, compare

git fetch
Download objects and refs from remote repository without merging. Safe way to see what others have committed.

git fetch origin

Keywords: fetch, download, remote, refs, safe

git remote
Manage remote repositories. Add, remove, or view remote connections to other repositories.

git remote add upstream https://github.com/original/repo.git

Keywords: remote, upstream, origin, connection

git tag
Create, list, or delete tags. Mark specific commits as releases or important milestones.

git tag -a v1.0.0 -m 'Release version 1.0.0'

Keywords: tag, release, version, milestone

git cherry-pick
Apply specific commits from another branch to current branch. Useful for hotfixes or selective merging.

git cherry-pick abc123

Keywords: cherry-pick, selective, hotfix, commit

git revert
Create new commit that undoes changes from previous commit. Safe way to undo without rewriting history.

git revert HEAD

Keywords: revert, undo, safe, history

git clean
Remove untracked files from working directory. Use -n for dry run, -f to force, -d for directories.

git clean -fd

Keywords: clean, untracked, remove, directory

git config
Set configuration options for Git. Configure user info, aliases, and behavior settings.

git config --global user.email 'you@example.com'

Keywords: config, settings, user, global, alias

git blame
Show who last modified each line of a file. Useful for tracking down when bugs were introduced.

git blame src/main.js

Keywords: blame, author, history, debug, tracking

git bisect
Binary search to find commit that introduced a bug. Efficiently narrow down problematic commits.

git bisect start && git bisect bad && git bisect good v1.0

Keywords: bisect, binary, search, bug, debug

git reflog
Show reference logs of HEAD changes. Recover lost commits or see complete history of branch changes.

git reflog

Keywords: reflog, recover, lost, commits, history

git worktree
Manage multiple working trees. Work on different branches simultaneously without switching.

git worktree add ../feature-branch feature-branch

Keywords: worktree, multiple, branches, simultaneous