Basic Git commands
The daily loop with Git: clone, branch, commit, push, merge. The commands you type every day.
Getting started
git init # new repository git clone <url> # copy a remote repository git config --global user.name "Name"
The daily loop
git status # where am I? git add file.txt # stage a file git add -p # stage hunk by hunk git commit -m "message" # record it git pull # fetch + merge git push # send it
git add -p lets you stage changes hunk by hunk — the key to
clean, atomic commits.
Branches
git switch -c my-branch # create and switch git switch main # change branch git merge my-branch # merge into the current branch git branch -d my-branch # delete once merged
Inspecting history
git log --oneline --graph --decorate git diff # unstaged changes git show <commit> # details of a commit
Ignoring files (.gitignore)
node_modules/ *.log .env /var/cache/
A file Git already tracks is not retroactively ignored: git rm --cached file, then commit, to drop it. git check-ignore -v file explains which rule ignores (or doesn't) a given file.
Stashing changes
git stash # set aside the current changes git stash push -m "message" # with a descriptive message git stash list # see every stash git stash pop # reapply the latest one and drop it git stash apply stash@{1} # reapply a specific stash without dropping it
Handy for switching branches quickly without committing unfinished work — stashing sets uncommitted changes aside and gives you a clean working directory.
Tags
git tag v1.0.0 # lightweight tag on the current commit git tag -a v1.0.0 -m "Version 1.0" # annotated tag (with message, author, date) git push origin v1.0.0 # push a specific tag git push origin --tags # push every tag
Prefer annotated tags (-a) for marking a published version: unlike lightweight tags, they store a message and an author, useful for tracing release history.
Modified vs. staged files
Git tracks three states: the working directory (your files as they are), the staging area (what git add placed there), and history (what a commit recorded). git diff compares working ↔ staging; git diff --staged compares staging ↔ the last commit.
Frequently asked questions
What is the difference between git switch and git checkout?
`git switch` only deals with branches (clearer, safer); `git checkout` does that too but also restores files. Prefer `switch` + `restore` on recent Git.
How do I undo a `git add`?
`git restore --staged <file>` unstages the file without changing its contents.
Does `git pull` merge or rebase?
Merge by default. To rebase: `git pull --rebase`, or set `git config --global pull.rebase true`.
A `git stash pop` conflicts — what now?
Resolve the conflicts as you would for a normal merge, then `git add` the affected files. The stash stays in the list until you explicitly drop it with `git stash drop` — nothing is lost if something goes wrong.
Thanks for the feedback!