Git Cheat Sheet #
π§ Configuration (One-time setup) #
git config user.name “Your Name”
git config user.email “you@example.com”
Set username and email for commits.
π Repository Setup #
git init
git init -b main
git remote add origin
Initialise a repository and connect it to a remote.
π Status & Inspection #
git status
git log
git show
git diff
Check repository state, history, commit details, and differences.
πΏ Branching & Navigation #
git branch
git checkout branch-name
git checkout -b new-branch
git switch branch-name
git switch -c new-branch
Create, list, and switch branches.
(git switch is the modern alternative to checkout.)
π¦ Staging & Committing #
git add .
git commit -m “message”
git rm file.txt
Stage changes, commit them, or remove tracked files.
π Sync with Remote #
git fetch
git pull origin branch-name
git push
git push –set-upstream origin branch-name
git push origin main -f
Fetch, pull, and push changes between local and remote.
β οΈ Force push (-f) overwrites remote history.
π§ History & Branch Integration #
git merge branch-name
Merge the specified branch into the current branch.
- Preserves full branch history
- Creates a merge commit (unless fast-forward)
- Safe for shared branches
Use when you want to keep context of parallel work.
git rebase branch-name
Reapply current branch commits on top of another branch.
- Creates a linear history
- Rewrites commit hashes
- Do not rebase shared branches
Use when you want a clean commit history.
β»οΈ Reset (Undo Commits) #
git reset –soft
Move HEAD to <commit>.
- Keeps changes staged
- Keeps file changes
Use to redo commit messages or squash commits.
git reset
(Default = --mixed)
- Moves HEAD to
<commit> - Unstages files
- Keeps file changes
Use to undo commits but keep work.
git reset –hard
- Moves HEAD to
<commit> - Deletes all local changes
- Working directory matches the commit
β οΈ Dangerous β changes are permanently lost.
π Restore Files (Modern Way) #
git restore file.txt
Restore file contents without switching branches.
Switch and Restore #
git switchβ for changing or creating branchesgit restoreβ for restoring files
| Action | Old way (checkout) | New way (switch / restore) |
|---|---|---|
| Switch to an existing branch | git checkout branch-name | git switch branch-name |
| Create and switch to a new branch | git checkout -b new-branch | git switch -c new-branch |
| Switch to a branch from another base | git checkout -b new-branch develop | git switch -c new-branch develop |
| Restore a file to last commit | git checkout -- file.txt | git restore file.txt |
πΉ Command Summary #
| Command | Purpose |
|---|---|
git switch | Only for switching or creating branches |
git restore | Only for restoring file contents |
git checkout | Old command that can do both, but can be confusing |
π Hugo #
hugo server
Run Hugo local development server.