This chapter follows How Git works.
These commands assume a repository already exists. To connect a new project to GitHub, see Working with remotes.
Identity and configuration
Commits record name and email. Set them once globally or per repository:
git config --global user.name "Your Name"git config --global user.email "you@example.com"On GitHub you can hide your address with a noreply email from profile settings. If GitHub rejects pushes because the email is private, configure that noreply address locally:
git config --global user.email "12345678+noreply@users.noreply.github.com"git config --global user.emailOmit --global to set values only for the current repo. View settings with git config --list or git config user.email.
Starting a repository and the daily loop
git initgit statusgit add .git commit -m "Initial commit"git status shows untracked files, staged changes, and branch state. git add path or git add . stages changes into the index. git commit -m "message" creates a snapshot on the current branch.
Commit messages should explain why, not only what changed. Small, frequent commits are easier to review and revert than one giant commit at the end of the week.
Removing and renaming files
git rm obsolete.txtgit rm --cached secret.local.envgit mv old-name.ts new-name.tsgit rm deletes from the working tree and stages the deletion. --cached removes from the index only (keeps the file on disk). Prefer git mv over manual rename plus add so history and renames stay clearer.
Inspecting history and diffs
git log --onelinegit log --graph --decorate --onelinegit show HEADgit diffgit diff --cachedgit diff HEADgit log lists commits on the current branch. git show displays one commit, including its patch. git diff compares working tree to index; --cached compares index to last commit; git diff HEAD is everything that would go into a commit if you staged it all.
Compare branches or paths:
git diff main feature -- src/app.tsCommitting part of a file
Not every hunk in a file must land in the same commit:
git add --patch file.txtGit walks hunks interactively: y stage, n skip, s split further. Useful when one file mixes a bugfix and an unrelated refactor.
Branches
List, create, switch, delete, and rename:
git branchgit branch -agit branch -vgit branch feature/logingit checkout feature/logingit checkout -b feature/logingit branch -d feature/logingit branch -D feature/logingit branch -m old-name new-name-a includes remote-tracking branches; -v shows the latest commit on each branch. -d refuses to delete unmerged work; -D forces deletion. You cannot delete the branch you are on—switch first.
Modern commands split checkout’s jobs:
git switch feature/logingit switch -c feature/logingit restore --staged file.txtgit restore file.txtgit switch moves HEAD to another branch. git restore copies from the repository to the index and/or working tree (for example to unstage or discard local edits).
Use short branch names with a team convention, such as feat/115/email-login or bug/123/menu-null.
Tags
Lightweight tags are branch-like refs; annotated tags store a message and signer metadata:
git tag v1.0.0git tag -a v1.0.0 -m "First production release"git push origin v1.0.0git push origin --tagsRelease workflows often require annotated tags on main.
Ignoring files
Git tracks tracked, untracked, and ignored paths. Place .gitignore at the repo root (or in subfolders) with patterns:
| Pattern | Effect |
|---|---|
.project |
Any file or folder named .project |
bin/ |
Directories named bin |
*.class |
Files with extension class |
**/bin |
bin directories at any depth |
Ignored files never appear in git add unless you force-add them. Typical ignores: build output, dependencies, IDE metadata, local env files.
Next: Working with remotes.