Git Command Practice
Git stores snapshots and moves branch pointers; the CLI is how you inspect and rewrite that history. This track drills status, add, commit, branch, rebase, restore, and worktrees as typed porcelain — the same verbs you need when a GUI is not available.
Command Reference
branches
| Command | Description |
|---|---|
git branch | List local branches and show the current branch. |
git branch <name> | Create a new branch without switching to it. |
git branch -r | List all remote-tracking branches in the repository. |
git switch -c <branch> | Create a new branch and switch to it. |
git checkout - | Quickly switch back to the previously checked-out branch. |
git switch <branch> | Switch to an existing branch. |
git branch -vv | List local branches with commit details and tracking upstream branches. |
git checkout <branch> -- <file> | Checkout a specific file version from another branch into the working tree. |
git merge <branch> | Merge another branch into the current branch. |
basics
| Command | Description |
|---|---|
git status | Show the working tree status and staged changes. |
git clone <url> | Clone a remote repository to your local machine. |
git init | Initialize a new Git repository in the current directory. |
history
| Command | Description |
|---|---|
git log -n <num> | Limit commit history output to the last N entries. |
git log --oneline | Show commit history as one compact line per commit. |
git log --grep=<pattern> | Filter commits by commit message matching a search pattern. |
git log -p | Show line-by-line diff patch changes over time for commits. |
git log --pretty=oneline --graph --decorate --all | Display an ASCII graph of commit history across all branches. |
git log --name-only | List the names of changed files for each commit. |
git log --no-merges --name-only | List changed files in commits excluding merge commits. |
git log --max-count=<num> | Limit commit history output to a maximum of N commits. |
git log --after="<date>" | Show commits created after a specific date. |
git log --before="<date>" | Show commits created before a specific date. |
git log --author=<pattern> | Filter commits by author name or email pattern. |
git log --graph --decorate | Show an ASCII commit graph for the current branch with reference tags. |
git reflog | Display the local history of HEAD and branch reference updates. |
git cherry-pick <commit> | Apply one specific commit onto the current branch. |
git rebase --continue | Continue a rebase after resolving conflicts. |
git rebase main | Replay current branch commits on top of main. |
commits
| Command | Description |
|---|---|
git commit -m "<message>" | Create a commit with an inline commit message. |
git commit --amend -m "<message>" | Modify the commit message of the latest commit. |
git commit --amend --no-edit | Add staged changes to the latest commit without changing its message. |
inspection
| Command | Description |
|---|---|
git diff | Show unstaged changes in the working tree. |
git diff --staged | Show changes staged for the next commit. |
git show <commit> | Show details and patch for a specific commit. |
git blame <file> | Show the last commit that changed each line of a file. |
remotes
| Command | Description |
|---|---|
git remote -v | List remotes with their fetch and push URLs. |
git pull | Fetch from the configured remote and merge into the current branch. |
git fetch | Download remote refs and objects without merging them. |
git push | Push local commits to the configured upstream branch. |
git push -u origin <branch> | Push a branch and set its upstream to origin. |
git remote add origin <url> | Add a remote named origin for a repository URL. |
staging
| Command | Description |
|---|---|
git add . | Stage all changed files under the current directory. |
git add <file> | Stage a specific file for the next commit. |
undo
| Command | Description |
|---|---|
git restore <file> | Discard unstaged changes in a specific file. |
git restore --staged <file> | Unstage a file while keeping its working tree changes. |
git reset --soft HEAD~1 | Undo the last commit while keeping changes staged. |
git reset --hard HEAD | Discard all tracked working tree and staged changes. |
workflow
| Command | Description |
|---|---|
git stash | Temporarily save uncommitted changes and clean the working tree. |
git stash pop | Apply the latest stash and remove it from the stash list. |
tags
| Command | Description |
|---|---|
git tag <name> | Create a lightweight tag at the current commit. |
git tag -a <name> -m "<message>" | Create an annotated tag with a message. |
worktrees
| Command | Description |
|---|---|
git worktree list | List all active working tree directories linked to the repository. |
git worktree add <dir> <branch> | Create a new working tree directory linked to a specific branch. |
git worktree remove <dir> | Remove a linked working tree directory. |
git worktree prune | Clean up administrative information for manually deleted working trees. |
git worktree move <dir> <new_dir> | Move a working tree directory to a new path location. |
submodules
| Command | Description |
|---|---|
git clone --recursive <url> | Clone a repository and automatically initialize all submodules. |
git submodule update | Update submodules to match the commit specified in the superproject. |
git submodule add <url> | Add a new external Git repository as a submodule. |
git submodule update --remote | Fetch and update submodules to their remote branch's latest commit. |
git submodule deinit <dir> | Unregister a submodule from the repository configuration. |
config
| Command | Description |
|---|---|
git config --global alias.<alias> <command> | Define a global Git command shortcut alias. |
cleanup
| Command | Description |
|---|---|
git clean -fdn | Preview untracked files and directories that would be removed. |
git clean -fd | Remove untracked files and directories from the working tree. |
Key Use Cases
- See what will be committed before you write a snapshot
- Rewrite the last commit message without changing the tree
- Restore a file you deleted in the working tree
- Rebase a feature branch onto updated main
- Park unfinished work with stash and come back later
Frequently Asked Questions
When should I use git commit --amend instead of a new commit?
Amend rewrites HEAD. Use it only for the commit you just made and have not pushed, or when you own the branch and will force-push on purpose.
What is the difference between git restore and git checkout?
restore changes files or the index. switch/checkout move HEAD. Older git used checkout for both, which is why this track treats them as separate prompts.
Is rebase safer than merge?
Rebase replays your commits on a new base and rewrites hashes. Merge adds a join commit and keeps history. Neither is always safer; this track tests when each command is the one you type.
Why practice the Git CLI if I use a GUI?
Hooks, CI, and ssh sessions still expect porcelain. A GUI click does not train git rebase --onto or git restore --staged.
Ready to master Git commands?
Test your muscle memory with our spaced-repetition quiz system. Free forever.
