Git & Terminal Cheatsheet

Git Basics

Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

First-Time Setup (git config)

git config --global user.name  "Ada Lovelace"
git config --global user.email "ada@example.com"
git config --global init.defaultBranch main   # new repos start on main
git config --global core.editor "code --wait" # editor for commit messages
git config --global pull.rebase true          # rebase instead of merge on pull
git config --list --show-origin               # see every setting and its file

--global writes ~/.gitconfig. Without it, settings apply to the current repo only (.git/config), which is how you use a work email in one repo.

Aliases

git config --global alias.st "status -sb"
git config --global alias.co switch
git config --global alias.br branch
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.lg "log --oneline --graph --decorate --all"
# now: git st, git lg, ...

Starting a Repository

git init                    # turn the current folder into a repo
git clone <url>             # copy an existing repo
git clone <url> myapp       # clone into a folder named myapp
git clone --depth 1 <url>   # shallow clone, latest snapshot only (fast, CI)

The Everyday Loop

git status                  # what is changed, staged, untracked
git status -sb              # the compact version
git diff                    # unstaged changes
git diff --staged           # what will go into the next commit
git add src/app.js          # stage one file
git add .                   # stage everything (respects .gitignore)
git add -p                  # stage hunk by hunk, review as you go
git commit -m "Add login"   # record the staged changes
git commit -am "Fix typo"   # stage all TRACKED files and commit in one step
StateMeaning
untrackednew file Git has never seen
modifiedtracked file changed, not staged
stagedqueued for the next commit
committedrecorded in history

.gitignore Patterns

One pattern per line in a .gitignore at the repo root (nested ones work too):

PatternIgnores
*.logevery .log file, anywhere
node_modules/any directory named node_modules
/distdist at the repo root only
build/**/*.map.map files anywhere under build/
.env*.env, .env.local, ...
!.env.exampleexception: do NOT ignore this one
*.py[co].pyc and .pyo files
git check-ignore -v .env    # WHY is this file ignored (which rule, which file)
git status --ignored        # list ignored files

Untracking a Committed File

.gitignore only affects untracked files. If a file was already committed (a .env, a build folder), untrack it without deleting it from disk:

git rm --cached secrets.env         # stop tracking, keep the local file
git rm -r --cached dist/           # same for a directory
echo "secrets.env" >> .gitignore
git commit -m "Stop tracking secrets.env"

If the file held real credentials, rotate them. Removing it from the latest commit does not remove it from history (that needs git filter-repo or BFG).