Knowledge base
A searchable reference for the git commands in the book.
Each article explains one command and shows an example with real output. Search by name, or browse the groups below. Every article links back to the chapter where the topic is covered in full.
Setup and the repository
5- git config: name, email, and settingsgit config stores git settings: the name and email that sign every commit, the editor for commit messages, and every other key.
- Git aliases: short names for long commandsAn alias is your own short name for a git command, set with git config.
- Line endings: LF, CRLF and .gitattributesLinux and macOS end a line of text with one character (LF), Windows with two (CR and LF).
- git init: turn a folder into a repositorygit init turns an ordinary folder into a repository: it creates a hidden .git directory inside, and that is where the whole history of the project will live.
- git clone: copy someone else's repositorygit clone takes a whole existing repository: the full history, every branch, and the files of the latest commit.
Commits
7- git status: what is going on in the repositorygit status answers the question "where am I and what have I got": which branch you are on, which files changed, and what is already picked for the next commit.
- git add: put changes into the indexgit add moves changes from the working directory into the index (staging area), the set that goes into your next commit as a whole.
- git commit: record a snapshotgit commit saves whatever sits in the index as a separate snapshot: with an author, a time, your message, and a unique hash.
- git commit --amend: fixing the last commitgit commit --amend replaces the last commit with a new one: with a corrected message, with a file you forgot, or with both at once.
- Commit messages: how to write themA commit message tells whoever opens the history six months later why the change was made.
- git rm and git mv: deleting and renaming under git's watchgit rm deletes a file and puts that deletion straight into the index; git mv renames one and does the same.
- git hooks: why a commit will not go throughYou type git commit, and instead of a new commit the terminal shows a message from a linter or a check, and no commit is made.
History and changes
6- git log: the history of a projectgit log shows the history of a repository: commits from the newest down to the oldest, each with its author, date, and message.
- git diff: what exactly changedgit diff shows the difference line by line: which lines went away, which ones appeared, and where in the file it happened.
- git show: look at one commit in fullgit show prints one object from the repository in full: the commit message, the author, the date and the whole diff of that commit.
- git blame: who changed each line, and whengit blame <file> prints the file line by line and attaches to every line the commit, the author and the date on which that line took its current shape.
- git bisect: find the commit that broke the projectgit bisect binary-searches for the first commit that broke the project: you name a good commit and a bad one, and git halves the range until a single culprit is left.
- .gitignore: what git should not notice.gitignore is a file listing everything git must ignore: temporary files, dependency directories, build artifacts, local secrets.
Undoing changes
8- git restore: put a file back and take it out of the indexgit restore undoes things at the file level: with --staged it takes a file out of the index, without the flag it returns the file to the way the last commit has it.
- git reset: move the branch pointer backgit reset moves the pointer of the current branch to another commit, and the modes --soft, --mixed and --hard decide what happens to the index and to your files.
- git revert: undo a commit with a new commitgit revert creates a new commit that undoes the changes of the one you name, while the commit being undone stays in the history right where it was.
- git clean: deleting untracked filesgit clean deletes the files git knows nothing about from your working directory: build artifacts, temporary files, an archive you unpacked in the wrong place.
- git stash: put unfinished work asidegit stash takes your unfinished edits off the working directory, files them in a separate list and brings your files back to the state of the last commit.
- git reflog: the log of HEAD movementsgit reflog lists every place HEAD has stood and keeps the hashes of commits that have already vanished from git log.
- How to undo the last commit"Undo the commit" is four different operations, and two questions pick between them: did the commit go to the server, and what should happen to its changes.
- How to restore a deleted fileWhere a deleted file went, and how to get it back, depends on the step at which it vanished.
Branches and merges
9- git branch: listing, creating and deleting branchesgit branch lists the branches in a repository, creates a new one, and deletes one you no longer need.
- git switch: moving between branchesgit switch <branch> moves you to another branch and rewrites the files in your working directory to match its last commit.
- git checkout: one command with three jobsgit checkout does three different things: it moves you between branches, pulls files out of a commit, and parks you on a commit with no branch attached.
- main and master: two names for the main branchmain and master are two names for the main branch of a repository, and there is no technical difference between them: both work exactly like any other branch.
- git merge: merging branchesgit merge <branch> merges the branch you name into the branch you are standing on.
- Merge conflicts: how to work through oneA merge conflict happens when two branches changed the same line in different ways, and git will not decide for you which version is right.
- git cherry-pick: moving one commit into the current branchgit cherry-pick <hash> takes the changes of a single commit from another branch and applies them to the branch you are standing on.
- detached HEAD: standing on a commit, not on a branchA detached HEAD means HEAD points straight at a commit instead of at a branch name, so new commits have nothing to attach to.
- git rebase: moving commits on top of someone else'sgit rebase rewrites your commits as if you had started work after your colleague finished theirs.
Remotes
12- git remote: the link to a remote repositorygit remote manages the list of addresses your repository exchanges commits with.
- SSH keys for GitHubAn SSH key is a pair of files that replaces the password when you talk to GitHub: you hand the public half to the server and keep the private half to yourself.
- git push: send commits to the servergit push copies commits from your local branch to the remote repository and moves the branch there forward.
- git push --force: rewriting history on the servergit push --force replaces the branch on the server with your local version, even if the commits that were sitting there become unreachable as a result.
- git pull: take other people's changesgit pull downloads new commits from the server and merges them straight into your current branch.
- git fetch: see what is new on the servergit fetch downloads fresh commits from the server without touching your branch or the files on disk.
- Pull requests and merge requestsA pull request is a proposal to merge your branch into the main one, presented as its own page with a discussion attached.
- A draft pull request: showing work earlyA draft pull request is the same pull request, only flagged as not ready to merge.
- A protected branch: why the push to main is rejectedYou try to push a commit straight into main, and the server refuses: the branch is protected, and you cannot write to it directly.
- Forks: your own copy of someone else's repositoryA fork is a copy of someone else's repository made under your own GitHub account, one that you are allowed to write to.
- git submodule: cloned it, and the folder is emptyA submodule is a link to another repository nested inside yours.
- git tag: marking versions and releasesA tag is a permanent name for one particular commit, and unlike a branch it never moves.