Git Command Cheatsheet
A quick reference for every Git command used in this course, organised by task. Commands are shown in the context of the A2A starter project where relevant.
Setup & Configuration
# Set your identity (required before your first commit)git config --global user.name "Your Name"git config --global user.email "you@example.com"
# Set default branch name to maingit config --global init.defaultBranch main
# Set VS Code as your default editorgit config --global core.editor "code --wait"
# Set VS Code as your merge conflict toolgit config --global merge.tool vscode
# View all configurationgit config --list
# View a single settinggit config user.emailStarting a Repository
# Initialise a new repo in the current directorygit init
# Clone a remote repositorygit clone https://github.com/fischer3-net/git-github-security-learning.git
# Clone and rename the local directorygit clone https://github.com/YOUR-USERNAME/repo.git my-folderStaging & Committing
# Check working directory and staging area statusgit status
# Stage a specific filegit add agents/search/main.py
# Stage all changes in the current directorygit add .
# Stage changes interactively — choose individual hunksgit add -p
# Unstage a file (keep changes in working directory)git restore --staged agents/search/main.py
# Commit staged changesgit commit -m "feat(search-agent): add mock search specialist agent"
# Commit with a multi-line message (opens your editor)git commit
# Amend the most recent commit message (before pushing)git commit --amend -m "corrected commit message"
# Amend to add a forgotten file (before pushing)git add forgotten-file.pygit commit --amend --no-editInspecting History
# View the commit loggit log
# Compact one-line loggit log --oneline
# Compact log with branch graphgit log --oneline --graph
# Compact log with all branches and remote refsgit log --oneline --graph --all --decorate
# Show the last N commitsgit log --oneline -5
# Show full details of a specific commitgit show abc1234
# Show full details of the most recent commitgit show HEAD
# Show who last changed each line of a filegit blame agents/orchestrator/main.pyDiffing
# Show unstaged changes (working directory vs staging area)git diff
# Show staged changes (staging area vs last commit)git diff --staged
# Show changes between two commitsgit diff abc1234 def5678
# Show changes between two branchesgit diff main..feat/search-agent
# Show changes introduced on a branch since it diverged from maingit diff main...feat/search-agent
# Show only the names of changed filesgit diff --name-only main..feat/search-agentBranching
# List local branches (* marks the active branch)git branch
# List local and remote branchesgit branch -a
# List branches with their latest commitgit branch -v
# Create a new branch (stays on current branch)git branch feat/search-agent
# Switch to an existing branchgit switch main
# Create and switch in one stepgit switch -c feat/search-agent
# Rename a local branchgit branch -m old-name new-name
# Delete a branch that has been mergedgit branch -d feat/search-agent
# Force-delete an unmerged branchgit branch -D feat/search-agent
# Compare commits on a feature branch not yet in maingit log main..feat/search-agentMerging
# Merge a branch into your current branchgit switch maingit merge feat/search-agent
# Merge with an explicit merge commit (no fast-forward)git merge --no-ff feat/search-agent
# Abort a merge in progress (e.g., during a conflict)git merge --abort
# After resolving conflicts, mark them as resolved and finish the mergegit add orchestrator/main.pygit merge --continueResolving Merge Conflicts
When Git pauses with a conflict, the affected file will contain markers:
<<<<<<< HEAD "echo": os.getenv("ECHO_AGENT_URL", "http://localhost:8001"),======= "echo": os.getenv("ECHO_AGENT_URL", "http://localhost:8001"), "search": os.getenv("SEARCH_AGENT_URL", "http://localhost:8002"),>>>>>>> feat/search-agent- Edit the file to the version you want — remove all three marker lines
- Stage the resolved file:
git add <file> - Complete the merge:
git merge --continue(orgit commit)
Rebasing
# Replay your branch commits on top of maingit switch feat/calculate-agentgit rebase main
# Rebase onto upstream/main (when syncing a fork)git fetch upstreamgit rebase upstream/main
# Abort a rebase in progressgit rebase --abort
# Continue after resolving a conflict during rebasegit add <resolved-file>git rebase --continue
# Interactive rebase — edit, squash, or reorder the last N commitsgit rebase -i HEAD~3Undoing Changes
# Discard unstaged changes to a file (irreversible)git restore agents/search/main.py
# Discard all unstaged changes in the working directorygit restore .
# Unstage a file (keep the changes)git restore --staged agents/search/main.py
# Undo the last commit, keep changes stagedgit reset --soft HEAD~1
# Undo the last commit, keep changes unstagedgit reset HEAD~1
# Undo the last commit and discard changes entirely (irreversible)git reset --hard HEAD~1
# Create a new commit that reverses a previous commit (safe for shared branches)git revert abc1234
# Revert the most recent commitgit revert HEADStashing
# Stash current uncommitted changesgit stash
# Stash with a descriptive messagegit stash push -m "WIP: search agent timeout handling"
# List all stashesgit stash list
# Apply the most recent stash (keeps it in the stash list)git stash apply
# Apply and remove the most recent stashgit stash pop
# Apply a specific stashgit stash apply stash@{2}
# Delete the most recent stashgit stash drop
# Delete all stashesgit stash clearRemotes
# List configured remotesgit remote -v
# Add a remote (e.g., upstream for a fork)git remote add upstream https://github.com/fischer3-net/git-github-security-learning.git
# Rename a remotegit remote rename origin old-origin
# Remove a remotegit remote remove upstream
# Fetch all changes from a remote (doesn't modify local branches)git fetch origingit fetch upstream
# Fetch and merge (fetch + merge in one step)git pull origin main
# Fetch and rebase instead of mergegit pull --rebase origin mainPushing
# Push a branch to origin for the first timegit push -u origin feat/search-agent
# Push subsequent commits (after -u is set)git push
# Push to a specific remote and branchgit push origin feat/search-agent
# Delete a remote branchgit push origin --delete feat/search-agent
# Force push (only use on your own branches, never main)git push --force-with-lease origin feat/calculate-agentSyncing a Fork
The two-remote model used in Module 07: origin is your fork, upstream
is the original repository.
# Add upstream once (if not already configured)git remote add upstream https://github.com/fischer3-net/git-github-security-learning.git
# Sync your fork's main with upstreamgit fetch upstreamgit switch maingit merge upstream/maingit push origin main
# Update a feature branch with the latest upstream/maingit switch feat/calculate-agentgit fetch upstreamgit rebase upstream/maingit push origin feat/calculate-agent --force-with-leaseTagging
# Create an annotated tag (use for releases)git tag -a v1.0.0 -m "v1.0.0 — Initial release of the A2A system"
# List all tagsgit tag
# Show details of a taggit show v1.0.0
# Push a specific tag to origingit push origin v1.0.0
# Push all tags to origingit push origin --tags
# Delete a local taggit tag -d v1.0.0
# Delete a remote taggit push origin --delete v1.0.0The Feature Branch Workflow
The full cycle used in every module:
# 1. Start from an up-to-date maingit switch maingit pull origin main
# 2. Create a feature branchgit switch -c feat/search-agent
# 3. Make commitsgit add .git commit -m "feat(search-agent): add mock search specialist agent"
# 4. Push the branch to your remotegit push -u origin feat/search-agent
# 5. Open a Pull Request on GitHub (Module 03)# ... review, approval, CI pass ...
# 6. After merge, clean up locallygit switch maingit pull origin maingit branch -d feat/search-agentQuick Reference
| Task | Command |
|---|---|
| Check status | git status |
| Stage all | git add . |
| Commit | git commit -m "message" |
| View log | git log --oneline --graph |
| Create & switch branch | git switch -c feat/name |
| Switch branch | git switch main |
| Delete merged branch | git branch -d feat/name |
| Delete remote branch | git push origin --delete feat/name |
| Fetch from upstream | git fetch upstream |
| Push branch (first time) | git push -u origin feat/name |
| Force push safely | git push --force-with-lease |
| Stash changes | git stash / git stash pop |
| Undo last commit (keep changes) | git reset HEAD~1 |
| Revert a pushed commit | git revert HEAD |
| Create a release tag | git tag -a v1.0.0 -m "message" |
| Push a tag | git push origin v1.0.0 |
Related
- Branch Naming Reference — naming conventions for all branch types in this project
- GitHub CLI Cheatsheet — equivalent operations using the
ghCLI - Module 01 · Repositories & Commits — staging, committing, and inspecting history
- Module 02 · Branching & Merging — branching, merging, and conflict resolution
- Module 07 · Collaboration at Scale — the two-remote fork workflow