Git Cheatsheet
Essential Commands for Developers
Repository Setup
git init
Create a brand-new, empty Git repository in the current folder.git clone <url>
Download an existing repository (with all its history) from a remote server like GitHub.git config --global user.name "Name"
Set your name for all commits you make on your machine.git config --global user.email "email"
Set your email address for all commits you make on your machine.
Basic Commands
git status
Show which files are new, modified, or staged for the next commit.git add <file>
Mark a specific file as ready to be included in the next commit.git add .
Stage all modified and new files in the current directory.git commit -m "msg"
Save staged changes permanently in the repository with a message.git commit -am "msg"
Stage and commit all tracked file changes in one step (skips new files).git log
View the history of commits with author, date, and message.git diff
See exact changes (line by line) that have been made but not yet staged or committed.
Branching
git branch
List all branches in your repository.git branch -a
List all branches, including remote branches on servers like GitHub.git branch <name>
Create a new branch (but don’t switch to it yet).git checkout <branch>
Switch to another branch and update your working files.git checkout -b <name>
Create a new branch and immediately switch to it.git merge <branch>
Combine changes from the named branch into your current branch.git branch -d <name>
Delete a branch (if it has been merged or is no longer needed).git fetch --prune
Remove remote-tracking branches that no longer exist on the remote (keeps your branch list clean).
Remote Operations
git remote
List remote repositories your project is connected to.git remote -v
Show remote repository names along with their URLs.git push <remote> <branch>
Upload your local branch commits to a remote repository.git pull <remote> <branch>
Fetch and merge changes from a remote branch into your current branch.git fetch
Download changes from a remote without merging them. Lets you review before applying.
Undo Changes
git reset <file>
Unstage a file that you had previously marked withgit add
.git reset --hard
Completely reset your working directory and index to the last commit (discard ALL changes).git checkout <file>
Discard changes in a file and restore it to the last committed version.git revert <commit>
Create a new commit that undoes the changes introduced by a specific commit.
Advanced
git stash
Temporarily save changes without committing, so you can switch branches or pull updates.git stash pop
Re-apply the most recently stashed changes to your working directory.git rebase <branch>
Move or combine commits onto another branch, keeping history cleaner.git tag <name>
Create a snapshot (tag) to mark a release or milestone in history.git log --oneline
Show commit history in a compact, one-line-per-commit format.