Skip to main content
graphwiz.aigraphwiz.ai
← Back to Cheatsheets

Git CLI Cheat Sheet

Git CLI Cheat Sheet

Quick reference for the most commonly used Git commands for branching, merging, and for productivity.

CategoryDescriptionCommand
ConfigurationUser name, email, global configgit config --global user.name "John Doe"
git config --global user.email "john@example.com"
BranchingCreate, switch, delete branchesgit checkout -b feature
git checkout -b bugfix
BranchingList branchesgit branch
StashSave changesgit stash save "work in progress"
git stash save -u
SynchronizationPush changes to remote, fetch latest changesgit fetch origin
git pull
StatusShow working tree statusgit status
git status -s (shows staged changes)
Remote ManagementAdd remote origingit remote add origin <url>
git remote -v
HistoryView commit historygit log --oneline
git log --oneline -n
git log -p patch-1 --pretty
git log -p --graph
git log --stat
git log --all
git log --follow --log
HistoryFollow the tail of commit in real-timegit log --oneline --decorate
HistoryShow commit loggit log --oneline

Basic Commands

CommandDescriptionExample
git initInitialize new repositorygit init
git clone <url>Clone repositorygit clone https://github.com/user/repo.git
git add <files>Stage changesgit add . or git add -p
git commit -m "msg"Commit changesgit commit -m "Initial commit"
git commit --amendModify last commitgit commit --amend -m "Fix typo"
git reset <file>Discard changesgit reset HEAD --hard
git rm <file>Remove filegit rm filename.txt
git mv <old> <new>Move/rename filegit mv old.txt new.txt
git checkout <branch>Switch branchesgit checkout main
git checkout develop
git checkout -b <branch>Create and switchgit checkout -b bugfix
git merge <branch>Merge branchesgit merge main feature
git merge --no-ff bugfix
git branch -d <name>Delete branchgit branch -d feature/branch
git branch -D bugfix/branch-name
git branch -D <name>Delete remote-tracking branchgit branch -r origin/remote
git branch -m <name>Rename branchgit branch -m "message"
git branch -m "switch to progress branch"
git branchDelete local branchgit branch -d local-branch-name
git branch -r origin/remote <name>Push remote branch and pull remote changesgit branch --set-upstream origin/main
git branch --set-upstream origin <branch-name>Set upstream branchgit branch --set-upstream origin/main

Branching Strategies

Use branches to isolate feature development and create feature branches for bug fixes.

StrategyCommand
Feature Branchgit checkout -b feature/login-page
Bug Fix Branchgit checkout -b bugfix/loading-crash
Hotfix Branchgit checkout -b hotfix/new-header
Release Branchgit checkout -b release/v2.0
Experimentalgit checkout -b experiment/new-ui

| Git Flow | Enable Git Flow for Linter | git flow init |

git branch --set-upstream-to=origin/main login-page
git flow init
```text

```bash
git branch -u origin/main features/login-page
```text

---

## Branch Management

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git branch` | List all branches | `git branch -a` |
| `git branch -d <name>` | Delete branch | `git branch -D branch-name` |
| `git branch -m <name>` | Rename branch | `git branch -m old-name new-name` |
| `git branch -r <name> <remote>` | List remote branches | `git branch -r` |
| `git branch -r -d <name>` | Delete remote branch | `git branch -r -d branch-name` |

## Stashing & Saving

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git stash` | Save local changes | `git stash save "work in progress"` |
| `git stash list` | List stashes | `git stash list` |
| `git stash apply` | Apply stash | `git stash pop` |
| `git stash apply` | Apply stash | `git stash apply` |
| `git stash drop` | Delete stash | `git stash drop` |
| `git stash clear` | Clear stash | `git stash clear` |

## Remote Operations

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git fetch` | Download objects and refs from remote | `git fetch origin` |
| `git pull` | Fetch and merge remote changes | `git pull origin main` |
| `git push` | Push local changes to remote | `git push origin main` |
| `git push --force` | Force push (use with caution) | `git push --force origin main` |
| `git push --set-upstream origin` | Push and set upstream tracking | `git push --set-upstream origin main` |
| `git push --all` | Push all branches | `git push --all --force` |
| `git clone --recurse-submodules` | Clone with submodules | `git clone --recurse-submodules <url>` |
| `git submodule update` | Update submodules | `git submodule update --init` |
| `git submodule update --remote` | Update from remote | `git submodule update --remote` |

## Tags

| Tag | Description | Example |
| ----------- | ------------- | --------- |
| `v1.0.0` | Lightweight tag for releases | `git tag v1.0.0` |
| `v2.0.0` | Lightweight annotated tag | `git tag -a v2.0.0` |
| `git tag -a v1.0.0 <message>` | List tags with message | `git tag -l "v1.0.0"` |
| `git tag -d v1.0.0` | Delete tag | `git tag -d v1.0.0` |
| `git push origin --tags` | Push tags | `git push origin --tags v1.0.0 v2.0.0` |
| `git push --follow-tags` | Push tags and follow them | `git push --follow-tags` |
| `git tag -d v1.0.0` | Delete tag locally and push to remote | `git push --follow-tags origin :main` |

## Rebase & Reset

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git rebase -i` | Interactive rebase | Not recommended |
| `git rebase <branch>` | Rebase current branch onto another | `git rebase main` |
| `git rebase --abort` | Abort ongoing rebase | `git rebase --abort` |
| `git rebase --continue` | Continue after resolving conflicts | `git rebase --continue` |
| `git rebase --skip` | Skip patch and rebase manually | `git rebase --skip` |

## Cherry-pick

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git cherry-pick <commit>` | Pick specific commits | `git cherry-pick <commit-hash>` |
| `git cherry-pick --continue` | Continue after conflicts | `git cherry-pick --continue` |
| `git cherry-pick --quit` | Quit cherry-pick | `git cherry-pick --quit` |
| `git cherry-pick --abort` | Abort cherry-pick | `git cherry-pick --abort` |
| ----------- | ------------- |
| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git clean -fd` | Clean untracked files | `git clean -fd` |
| `git clean -fx` | Clean ignored files | `git clean -fx` |

| `git gc` | Garbage collection | `git gc` |

## Bisect

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git bisect` | Binary search for commits | `git bisect start` <br>`git bisect good`<br>`git bisect bad`<br>`git bisect reset`<br>`git bisect replay`<br>`git bisect log`<br>`git bisect run <cmd...>`<br>`git bisect visualize` |
| `git blame` | Show what revision modified a line | `git blame <file>` |

## Worktree

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git worktree add` | Add new worktree | `git worktree add <path> <branch>` |
| `git worktree list` | List worktrees | `git worktree list` |
| `git worktree remove` | Remove worktree | `git worktree remove <path>` |
| `git worktree prune` | Prune deleted worktrees | `git worktree prune` |

## Reflog

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git reflog` | Show reference log | `git reflog`<br>`git reflog show`<br>`git reflog expire` |
| `git reset --hard HEAD@{n}` | Reset to previous commit | `git reset --hard HEAD~5` |
| `git reset --soft HEAD@{n}` | Reset but keep changes | `git reset --soft HEAD~5` |
| `git revert HEAD~{n}` | Undo commit but keep changes | `git revert HEAD~5` |

## Ignore

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git ignore` | Ignore files | Add `*.log` to `.gitignore`<br>`git ignore *.env` |
| `git check-ignore -v` | Check ignore status | `git check-ignore -v` |
| `git rm --cached` | Remove from index | `git rm --cached -r .env` |

## Submodules

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git submodule` | Manage submodules | `git submodule status` |
| `git submodule add <url>` | Add submodule | `git submodule add https://github.com/user/repo.git` |
| `git submodule update --init` | Initialize submodules | `git submodule update --init --recursive` |
| `git submodule update --remote` | Update submodules | `git submodule update --remote` |
| `git submodule foreach` | Run command in each submodule | `git submodule foreach 'git pull'` |

## SSH/GPG

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git ssh-key` | Manage SSH keys | `ssh-keygen -t ed25519 -C ed25519 -f ~/.ssh/id_ed25519`<br>`git config --global gpg.program gpg` |
| `git config --global gpg.program gpg2` | Configure GPG program | `git config --global gpg.program gpg2` |
| `git config --global commit.gpgsign true` | Enable GPG signing | `git config --global commit.gpgsign true` |
| `git config --global user.signingkey ~/.ssh/id_ed25519.pub` | Set signing key | `git config --global user.signingkey ~/.ssh/id_ed25519.pub` |

## CI/CD Integration

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git hooks` | Manage Git hooks | `git config --global core.hooksPath .githooks` |
| `pre-commit` | Run tests before commit | `.git/hooks/pre-commit` |
| `git commit --no-verify` | Skip pre-commit hook | `git commit --no-verify -m "message"` |

## Troubleshooting

| Command | Description | Example |
| ----------- | ------------- | --------- |
| `git fsck` | Check repository integrity | `git fsck` |
| `git prune` | Remove unreachable objects | `git prune` |
| `git count-objects -v` | Count objects | `git count-objects -v` |

---

## Tips

### Use Aliases for Efficiency

```bash
# Add common aliases to ~/.bashrc or ~/.zshrc
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
alias gco='git checkout'
alias gb='git branch'
alias gm='git merge'
```text

### Undo Local Changes

```bash
# Discard all local changes
git checkout -- .
git clean -fd
```text

### Find Who Changed a Line

```bash
# Find commits that modified a specific line
git log -p --follow -S <file> | git blame <file>
git log -p -- "<line>" <file>

# Find when a line was added
git log -p -S <file> | git blame <file>
```text

### Sync with Remote

```bash
# Fetch and rebase
git fetch origin
git rebase origin/main

# Pull with rebase
git pull --rebase origin main
```text

### Clean Start Over

```bash
# Remove untracked files and directories
git clean -fd
git clean -fx
```text

### Fix Commit Issues

```bash
# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Amend last commit message
git commit --amend -m "New message"

# Interactive rebase
git rebase -i HEAD~5
```text

### Work with Branches

```bash
# Create feature branch
git checkout -b feature/awesome-feature

# Push new branch
git push -u origin feature/awesome-feature

# Delete local branch
git branch -d feature/old-feature

# Delete remote branch
git branch -r origin/old-feature
```text

---

## Common Workflows

### Start New Feature

```bash
# Create and switch to new branch
git checkout -b feature/new-feature

# Stage changes
git add .
git commit -m "Add new feature"

# Push to remote
git push -u origin feature/new-feature
```text

### Fix a Bug

```bash
# Create hotfix branch
git checkout -b bugfix/issue-123

# Fix the issue
git add .
git commit -m "Fix issue #123"

# Push and merge
git push -u origin bugfix/issue-123
git checkout main
git merge bugfix/issue-123
git push
```text

### Release New Version

```bash
# Create release branch
git checkout -b release/v2.0.0

# Bump version
git add package.json
git commit -m "Bump to version 2.0.0"
git tag -a v2.0.0
git push origin v2.0.0
git push --tags
```text

## Configuration

### Set Global Config

```bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
```text

### Set Local Config

```bash
git config user.name "Your Name"
git config user.email "you@example.com"
```text

### View Config

```bash
git config --list
git config --global --list
git config --local --list
```text

---

## Troubleshooting

### Repository is Broken

```bash
# Remove .git and restore
rm -rf .git
git init
git remote add origin
```text

### Committed to Wrong Branch

```bash
# Reset to correct branch
git fetch origin
git reset --hard origin/main
```text

### Merges Going Wrong

```bash
# Abort merge
git merge --abort

# Reset to remote state
git fetch origin
git reset --hard origin/main
```text

### Accidental Commit

```bash
# Undo but keep changes
git reset --soft HEAD~1

# Change message
git commit --amend -m "Correct message"
```text

### Detached HEAD

```bash
# Find commits not in any branch
git log --all --oneline

# Reattach to a branch
git checkout <branch>
```text

### Large File in History

```bash
# Shallow clone (faster)
git clone --depth 1 <url>

# Full history
git clone <url>
```text

### Push Rejected

```bash
# Force push (dangerous)
git push --force origin main

# Safer: rebase and push
git rebase origin/main
git push
```text

## Next Steps

- [Docker CLI Cheat Sheet](/devops/docker-cheatsheet/) — Container commands for modern development
- [Kubernetes kubectl Cheat Sheet](/devops/kubernetes-cheatsheet/) — Essential kubectl commands for orchestration
- [Helm CLI Cheat Sheet](/devops/helm-cheatsheet/) — Kubernetes package manager reference
- [Bash CLI Tools Cheat Sheet](/devops/bash-tools-cheatsheet/) — Unix command-line utilities