- Published on
- 4 min read
Git Cheat Sheet
- Authors
- Name
- Learna

Git tracks snapshots. As you work, changes may be in your working tree, staged in the index, or recorded in commits. Check which state a file is in before choosing an undo command:
working tree --git add--> index --git commit--> local history --git push--> remote
Config
Show configuration:
git config --list
Set your commit name globally:
git config --global user.name "Your Name"
Setup
Initialize a repository in the current directory:
git init
Clone a repository:
git clone <URL>
Stage
See staged and unstaged changes:
git status
Review unstaged changes:
git diff
Review staged changes:
git diff --staged
Stage selected files:
git add path/to/file
Stage all changes under the current directory, including new files:
git add .
Commit
Record the staged snapshot in local history:
git commit -m "Describe the change"
Stage and commit modifications to tracked files (new untracked files are not included):
git commit -a -m "Describe the change"
View recent commits:
git log --oneline
Amend the most recent local commit after staging the changes to include:
git commit --amend --no-edit
Amending replaces that commit and changes its ID. Do not amend commits already shared with others unless your team has agreed how to update the shared branch.
Branches
List local branches:
git branch
Create and switch to a branch:
git switch -c feature/search
Switch to an existing branch:
git switch main
Return to the previous branch:
git switch -
Rename the current branch to main:
git branch -m main
Delete a branch only after checking its work is no longer needed:
git branch -d feature/search
Remotes and sharing
Show configured remotes:
git remote -v
Add a remote:
git remote add origin <URL>
Fetch remote commits and references without integrating them into the current branch:
git fetch origin
Integrate the remote main branch into the current branch:
git merge origin/main
Push the local main branch and set its upstream (example assumes the branch is named main):
git push -u origin main
Pull updates for a branch with an upstream configured:
git pull
Abort an in-progress merge:
git merge --abort
Undo changes
Unstage a file and keep its working-tree edits:
git restore --staged path/to/file
Discard unstaged edits to a tracked file, restoring it from the index:
git restore path/to/file
The second command removes those edits from that file. Review git diff first; this operation is not an undo history you can rely on recovering from.
Move the current branch back one commit while keeping its changes staged:
git reset --soft HEAD~1
Reset changes local history. For a commit already shared on a remote branch, create a new commit that reverses its changes instead:
git revert <COMMIT_ID>
Stash
Temporarily stash tracked, staged and unstaged changes:
git stash push -m "work in progress"
Include untracked files too (-u does not include ignored files):
git stash push -u -m "work in progress"
List stashes:
git stash list
Reapply the newest stash and keep it in the list:
git stash apply stash@{0}
Apply the newest stash and remove it from the list if application succeeds:
git stash pop
Rebase
Rebase reapplies commits from the current branch on top of another branch. It rewrites those commits, so use it for local work or coordinate before rewriting commits others may already have. Example assumes the base branch is named main:
git fetch origin
git rebase origin/main
Worktrees
Create a new hotfix branch from main in a separate directory, keeping this working directory available (example assumes main exists):
git worktree add -b hotfix ../hotfix main
List worktrees:
git worktree list
For more detail, see the official Git documentation, git restore, git switch, and Stashing and Cleaning.