Decision guide: I changed a file, now what?
Beginner Core Git
Why this matters
Sections 2 and 5 gave you the commands. This page arranges them as decisions, so that the next time you save a file you know the next step without thinking. Print it; it is the whole of everyday Git on one page. The reference section has the same guide alongside its siblings (Decision guides).
The flowchart
The same thing as a table
| You just… | Ask | If yes | If no |
|---|---|---|---|
| saved a file | git status: is the branch right and the change what I meant? |
git diff |
wrong branch: lesson 12.7; wrong change: edit again or git restore <file> |
read git diff |
Does this belong in the next commit? | git add <file> |
leave it, or git stash it |
| staged | git diff --staged: complete, and about one thing? |
git commit -m "type: what (#n)" |
stage more, or stage part with git add -p, or unstage with git restore --staged |
| committed | Is it pushed, and does anyone need it now? | git pull, then git push |
keep working; push at the next pause |
| pushed | Is it wrong? | git revert <hash>, push |
done |
Three commands appear nowhere in the table because they are not everyday: reset --hard, push --force, rebase. If you feel you need one, read Section 12 first.
Special cases, briefly
- A new file:
git statusshows it as untracked;git addstarts tracking it; the rest is identical. - A deleted file: shows as deleted;
git add <file>orgit rm <file>stages the deletion. - A file that should not be there (generated, secret): do not add it; add it to
.gitignore(lesson 4.3). - A file changed by a tool (formatter, line endings, mode): check the diff size; restore it if you did not mean it (lesson 4.5).
- Several unrelated changes in one file:
git add -pto stage one at a time (lesson 5.3).