Photo by chansu shin on Unsplash

On this page

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:

Terminal window
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:

Terminal window
git config --global user.email "12345678+noreply@users.noreply.github.com"
git config --global user.email

Omit --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

Terminal window
git init
git status
git 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

Terminal window
git rm obsolete.txt
git rm --cached secret.local.env
git mv old-name.ts new-name.ts

git 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

Terminal window
git log --oneline
git log --graph --decorate --oneline
git show HEAD
git diff
git diff --cached
git diff HEAD

git 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:

Terminal window
git diff main feature -- src/app.ts

Committing part of a file

Not every hunk in a file must land in the same commit:

Terminal window
git add --patch file.txt

Git 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:

Terminal window
git branch
git branch -a
git branch -v
git branch feature/login
git checkout feature/login
git checkout -b feature/login
git branch -d feature/login
git branch -D feature/login
git 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:

Terminal window
git switch feature/login
git switch -c feature/login
git restore --staged file.txt
git restore file.txt

git 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:

Terminal window
git tag v1.0.0
git tag -a v1.0.0 -m "First production release"
git push origin v1.0.0
git push origin --tags

Release 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.

Search articles

Type a word to search published articles.

Open the search page