Git Commands

The daily set: setup, workflow, branching, remotes — and how to undo things.

Setup

git config --global user.name "Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

Start & clone

git init                  start a repo here
git clone <url>         copy a remote repo locally

Daily workflow

git status                what changed?
git diff                  unstaged changes in detail
git add <file>            stage a file (use . for all)
git commit -m "msg"       commit staged changes
git commit -am "msg"      stage tracked files + commit in one step

Branching

git branch                list branches (-a includes remotes)
git switch -c feat       create branch feat and switch to it
git switch main           switch back to main
git merge feat            merge feat into the current branch
git branch -d feat        delete merged branch (-D forces)

Remotes

git remote -v                    show remotes
git fetch                        download without merging
git pull                         fetch + merge (or rebase, per config)
git push                         upload commits
git push -u origin feat          push new branch and set upstream

Undo (in order of danger)

git restore <file>          discard unstaged changes (safe)
git restore --staged <file>  unstage, keep edits
git commit --amend          fix the last commit (message or files)
git reset --soft HEAD~1     undo last commit, keep staged
git reset --hard HEAD~1     undo last commit AND discard changes (dangerous)
git revert <sha>           safely undo a pushed commit (adds a new commit)

Log, stash, tags

git log --oneline --graph -10   compact history with branches
git stash                       shelve changes temporarily
git stash pop                   bring them back
git tag v1.0                    tag a release; git push --tags to publish

Frequently asked questions

I committed to the wrong branch. Now what?

Don't panic: git log to find the commit SHA, git switch to the right branch, git cherry-pick <sha>, then go back and git reset --hard HEAD~1 on the wrong branch (only if unpushed).

Merge or rebase?

Rule of thumb: rebase private branches before merging to keep history linear; never rebase commits someone else has already pulled.

How do I undo a pushed commit?

Use git revert — it adds a new commit that undoes the change, so everyone's history stays consistent. Reserve reset --hard + force-push for branches only you use.