Repositories, Staging, Commits and Config
Git tracks content through three areas (working tree, index and repository), and most everyday surprises come from forgetting which area a command touches, how ignore rules match, or which config level wins.
Key points
- 1
git addcopies a file's current content into the index; a commit records the index, not your working tree, so edits made aftergit addare not included until you add again. - 2
git status --shorthas two columns: index vs HEAD, then working tree vs index.AM,MM,ADandReach describe a precise state. - 3
git commit -aandgit add -ucover tracked files only;git add -Aalso stages new files and lets status detect renames. - 4
.gitignoreonly affects untracked files. To stop tracking a committed file, rungit rm --cachedand commit, and treat any committed secret as leaked. - 5
Ignore patterns: a leading or middle slash anchors to the
.gitignore's directory, a trailing slash matches directories, the last matching line wins, and you can't re-include a file whose parent directory is excluded (usedir/*instead). - 6
Config precedence runs system < global < local (< worktree) <
-con the command line.git config --show-originand--show-scopeshow where a value comes from. - 7
.gitattributesis committed with the project:text eol=lf,binary, diff and merge drivers, andexport-ignoreapply to everyone, unlike each developer'score.autocrlf.
Common traps
logs/followed by!logs/keep.txtkeeps the file ignored; writelogs/*then!logs/keep.txt.git clone --depth 1 ../repoignores--depthfor local paths (usefile://), and--depthimplies--single-branch.Git doesn't track empty directories, and it doesn't record renames; rename detection is content-based with a 50% similarity default.
Read the source
Test yourself on Repositories, Staging, Commits and Config
Ten questions, with the answer and explanation after each one.