105 cards
Git offers a spectrum of "undo" commands whose power increases from safe to destructive. The lightest touch is git reset --soft HEAD~1, which moves the HEAD pointer back one commit but leaves every change staged for an immediate recommit. The default git reset --mixed HEAD~1 (or just git reset HEAD~1) does the same mov...
Untracked files are handled separately from modifications, and git clean is the dedicated tool for removing them. By default it removes untracked files; adding -d extends it to untracked directories. A dry run with git clean -nd lists everything that would be deleted without touching the disk, which is the safest way t...
Interactive rebase is Git's primary tool for cleaning up commits before sharing them. Running git rebase -i HEAD~5 opens an editor listing the last five commits; you can reorder lines to change their order, change pick to reword to edit a message, edit to pause for amendments, or squash (and fixup) to combine a commit...
Merging is Git's other way of combining work, and it offers several flags that change how branches are combined. The default git merge feature performs a fast-forward when possible and otherwise creates a merge commit. To require a linear history, git merge --ff-only feature aborts if a fast-forward is not possible. To...
git stash provides a way to set work aside without committing it, useful when you need a clean working tree to switch branches, pull, or bisect. git stash push -m 'message' saves the current modifications onto a stack of stashes and resets the working tree. By default only tracked files are stashed; git stash push -u (...
Branch housekeeping is mostly a matter of choosing the right safety flag. git branch -m new-name renames the current branch (use -M to force-overwrite an existing name). Deletion uses git branch -d branchname, which refuses if the branch has unmerged commits; git branch -D removes it anyway. To delete the matching bran...
Git's history tools answer different questions, and choosing the right one saves time. git log --oneline --graph --decorate --all renders the branch topology in the terminal so you can see merges and divergence at a glance. For blame-style questions, git blame file annotates each line with the commit that last touched...
For mistakes that have already entered shared history, rewriting tools are needed. To remove a committed secret, use git filter-repo or the BFG Repo-Cleaner to scrub it from every commit, then force-push and rotate the secret itself. Such operations rewrite every commit's hash, so every collaborator must re-clone or ca...