15 Git - Cheatsheet
Git records changes in your project; GitHub is one place to share a Git repository. This quick reference assumes that you are inside a repository and have already configured Git. For the concepts behind these commands, see the Version Control chapter.
15.1 Inspect Before You Change Anything
| Task | Command | What to look for |
|---|---|---|
| See current branch and file state | git status |
Changed, staged, and untracked files |
| Inspect unstaged edits | git diff |
The lines you have changed |
| Inspect staged edits | git diff --staged |
What the next commit would include |
| See recent history | git log --oneline -5 |
Recent commits and messages |
Saving a file in your editor does not commit it. A commit records a snapshot of the staged changes, so inspect both the working-tree diff and the staged diff before you commit.
15.3 Correct a Local Mistake
| Situation | Command | Effect |
|---|---|---|
| Staged a file too early | git restore --staged path/to/file |
Removes it from staging; keeps your edits |
| Want to see where a file changed | git diff -- path/to/file |
Shows unstaged changes to that file |
| Want to discard unstaged edits to one file | git restore path/to/file |
Replaces its working copy with the staged version—inspect first |
Git normally tracks code and documentation; generated files and private credentials should not be added accidentally. Inspect git status and keep an appropriate .gitignore. If a credential was committed, revoke it: deleting the latest copy does not erase the earlier commit.