Back to Cheat Sheets
Git Commands
Common Git commands for version control and collaboration, with usage tips.
| Action | Command | Details |
|---|---|---|
| Initialize repository | git init |
Create a new Git repository in the current folder. Use this to start tracking a new project. |
| Clone repository | git clone <url> |
Download an existing repository from a remote source. Add --depth=1 for a shallow clone. |
| Check status | git status |
Show changed files in the working directory, what’s staged, unstaged, or untracked. |
| Add files | git add <file> |
Stage specific files for the next commit. Use 'git add -p' for interactive patch staging. |
| Add all files | git add . |
Stage all current changes (new, modified, deleted files) in the repo. |
| Commit changes | git commit -m 'message' |
Save staged changes with a descriptive message. Use '-a' to auto stage tracked files. |
| View commit history | git log --oneline --graph |
Display concise commit history with branch and merge structure. |
| Create new branch | git branch <name> |
Create a new branch to work on features or fixes isolated from main code. |
| Switch branch | git checkout <branch> |
Switch to another branch. Use 'git switch <branch>' in newer Git. |
| Merge branch | git merge <branch> |
Merge changes from another branch into the current one. |
| Delete branch | git branch -d <name> |
Delete a branch locally if fully merged; use '-D' to force. |
| Push changes | git push origin <branch> |
Upload local commits to the remote repository branch. |
| Pull changes | git pull origin <branch> |
Fetch and merge remote changes into your current branch. |
| Undo last commit | git reset --soft HEAD~1 |
Undo the last commit but keep changes staged. Use '--hard' to discard changes. |
| Stash changes | git stash |
Temporarily save uncommitted changes to a stack for later use. |
| Show stashes | git stash list |
View all saved stashes. |
| Apply stash | git stash apply [stash@{0}] |
Reapply changes saved in a stash without removing it. |
| Pop stash | git stash pop |
Reapply and remove the latest stash. |
| View remote repos | git remote -v |
List configured remote repositories with URLs. |
| Fetch remote updates | git fetch origin |
Download latest remote changes without merging. |
| Diff changes | git diff |
Show unstaged changes; add '--staged' for staged changes. |
| Tagging | git tag <name> |
Create a lightweight tag at the current commit. |
| Checkout file | git checkout -- <file> |
Discard changes in a file to last committed state. |
| Rebase branches | git rebase <branch> |
Reapply commits on top of another branch for a linear history. |