GIT

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 branches
  • git restore β†’ for restoring files
ActionOld way (checkout)New way (switch / restore)
Switch to an existing branchgit checkout branch-namegit switch branch-name
Create and switch to a new branchgit checkout -b new-branchgit switch -c new-branch
Switch to a branch from another basegit checkout -b new-branch developgit switch -c new-branch develop
Restore a file to last commitgit checkout -- file.txtgit restore file.txt

πŸ”Ή Command Summary #

CommandPurpose
git switchOnly for switching or creating branches
git restoreOnly for restoring file contents
git checkoutOld command that can do both, but can be confusing

πŸ›  Hugo #

hugo server

Run Hugo local development server.

Test the run from workflow on GitHub Actions #

Home | Technology