This page is a quick reference for the Git commands taught in the earlier lessons.
Create a repository
git init
Check the repository state
git status
Stage files for the next commit
git add README.md
git add .
Record a commit
git commit -m "Describe the change"
If the repository uses Conventional Commits, the message may look like:
git commit -m "feat: add login form"
git commit -m "fix: correct button disabled state"
Sign a commit
git commit -S -m "Signed commit message"
-S signs the commit cryptographically. For this to work, Git must be configured with a signing key. See the signing lesson for setup.
Enable automatic signing
git config --global commit.gpgSign true
Once enabled, every commit is signed without needing -S explicitly.
Verify a signature locally
git log --show-signature -1
This shows the signature details for the most recent commit.
Inspect changed content
git diff
git diff --staged
Inspect history
git log
git log --oneline
Switch to an existing branch
git switch main
Create a branch and switch to it
git switch -c feature-branch
Merge a finished branch
git switch main
git merge feature-branch
Undo a commit by creating a revert commit
git revert HEAD
git revert abc1234
git revert creates a new commit that undoes the changes from a previous commit.
This is safe for shared history because it adds to the history rather than rewriting it.
Undo recent local commits
git reset HEAD~1
This moves the current branch back one commit, keeping the changes in your working tree as unstaged modifications.
Use git reset --soft HEAD~1 to keep the changes staged instead.
Discard uncommitted changes
git restore file.txt
git restore .
git restore replaces the working tree files with the last committed version.
Use git restore --staged file.txt to unstage a file without discarding the changes.
View commit history with branches
git log --oneline --graph --all
This shows a compact, visual representation of all branches and how they relate:
* abc1234 (HEAD -> feature) Add new feature
| * def5678 (main) Fix bug in login
|/
* ghi9012 Initial commit
Apply a specific commit from another branch
git cherry-pick abc1234
git cherry-pick takes the changes from one commit and applies them as a new commit on the current branch.
This is useful when you need one specific fix from another branch without merging the entire branch.
Create and manage tags
git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
Tags mark specific points in history, commonly used for releases. Lightweight tags (git tag) just label a commit. Annotated tags (git tag -a) store additional metadata including the tagger and message.
Work with remote repositories
git remote -v
git remote add origin https://github.com/user/repo.git
git remote manages references to remote repositories. The name origin is the default name for the remote you cloned from.
Using GitHub CLI for auth and setup
gh auth login # authenticate interactively, handles SSH key setup
gh repo create # create a GitHub remote and push existing local commits
gh auth status # check current authentication status
GitHub CLI (gh) simplifies authentication, SSH key setup, and remote configuration in one interactive command. See the connecting lesson for details.