Markdown source

Git Commands I Actually Use Markdown source

Readable source view for humans. The raw Markdown endpoint remains available for crawlers and agent readers.

---
title: "Git Commands I Actually Use"
description: "The 20% of git that covers 95% of daily work — no theory, just commands."
kind: snippet
maturity: budding
confidence: high
origin: ai-drafted
author: "Agent"
directedBy: "krow"
tags: [git, reference]
published: 2026-03-20
modified: 2026-04-21
wordCount: 444
readingTime: 3
related: [building-krowdev-with-agents, agentic-coding-getting-started]
url: https://krowdev.com/snippet/git-commands-i-use/
---
## Agent Context

- Canonical: https://krowdev.com/snippet/git-commands-i-use/
- Markdown: https://krowdev.com/snippet/git-commands-i-use.md
- Full corpus: https://krowdev.com/llms-full.txt
- Kind: snippet
- Maturity: budding
- Confidence: high
- Origin: ai-drafted
- Author: Agent
- Directed by: krow
- Published: 2026-03-20
- Modified: 2026-04-21
- Words: 444 (3 min read)
- Tags: git, reference
- Related: building-krowdev-with-agents, agentic-coding-getting-started
- Content map:
  - h2: Daily
  - h2: Branching
  - h2: Checking History
  - h2: Undoing Things
  - h2: Working with Remotes
  - h2: Stashing
  - h2: Useful Aliases
  - h2: Sources
- Crawl policy: same canonical content is exposed through HTML, Markdown, and llms-full; no crawler-specific content gate.

## Daily

```bash
# What changed?
git status
git diff                    # unstaged changes
git diff --staged           # staged changes (about to commit)

# Commit
git add -p                  # stage hunks interactively — review what you're committing
git commit -m "message"

# Sync
git pull --rebase           # pull without merge commits
git push
```

## Branching

```bash
# Create and switch
git checkout -b feature/thing
git switch -c feature/thing   # modern equivalent

# Switch back
git checkout main
git switch main

# Delete after merge
git branch -d feature/thing   # safe — refuses if unmerged
git branch -D feature/thing   # force — deletes regardless
```

Branching becomes especially important with [agentic coding workflows](/guide/agentic-coding-getting-started/) — each AI agent runs in its own branch or worktree so they can't step on each other's work.

The review habit in [Reviewing AI-Generated Code](/guide/reviewing-ai-generated-code/) starts with `git diff`, and the larger project workflow in [What I Learned Building krowdev with AI Agents](/article/building-krowdev-with-agents/) depends on these commands staying boring and reliable.

## Checking History

```bash
# Recent commits
git log --oneline -10
git log --oneline --graph --all   # visual branch topology

# What changed in a commit?
git show abc1234
git show abc1234 --stat           # files only, no diff

# Who changed this line?
git blame src/lib/auth.ts

# Search commit messages
git log --grep="fix auth"

# Find when a string was added/removed
git log -S "functionName" --oneline
```

## Undoing Things

```bash
# Unstage a file (keep changes)
git restore --staged file.ts

# Discard local changes to a file
git restore file.ts

# Amend the last commit (message or content)
git add forgotten-file.ts
git commit --amend

# Undo last commit but keep changes staged
git reset --soft HEAD~1

# Undo last commit, unstage changes
git reset HEAD~1
```

## Working with Remotes

```bash
# See what's out there
git remote -v
git fetch --all

# Check divergence from upstream
git rev-list --left-right --count main...upstream/main
# Output: "18  29" means 18 ahead, 29 behind

# Rebase onto upstream
git fetch upstream
git rebase upstream/main
```

## Stashing

```bash
# Save work in progress
git stash
git stash push -m "wip: auth refactor"

# Get it back
git stash pop              # apply and remove from stash
git stash apply            # apply but keep in stash

# List stashes
git stash list
```

## Useful Aliases

Add to `~/.gitconfig`:

```ini
[alias]
  s = status --short
  l = log --oneline -20
  d = diff
  ds = diff --staged
  co = checkout
  cb = checkout -b
  amend = commit --amend --no-edit
  last = log -1 --stat
  branches = branch -a --sort=-committerdate
```

## Sources

- Git, [git-diff documentation](https://git-scm.com/docs/git-diff)
- Git, [git-restore documentation](https://git-scm.com/docs/git-restore)
- Git, [git-worktree documentation](https://git-scm.com/docs/git-worktree)