Git Course 0%

Decision guide: I changed a file, now what?

Beginner Core Git ≈ 5 min

What you will learn

  • The questions to ask after any edit, in order, and the command each answer leads to

After this lesson you can

  • I never wonder "what do I type now?" after changing a file

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

Decision guide: I changed a file, what now? A flowchart. Start: run git status. Question: is the change what you meant? If no: git restore the file, or edit again. If yes: run git diff and read it. Question: does it belong in the next commit? If no: leave it unstaged, or stash it. If yes: git add the file, then git diff --staged. Question: is the commit complete and about one thing? If no: add more files or stage part with add -p. If yes: git commit -m with a message and issue number. Question: ready to share? If yes: git pull then git push. Otherwise keep working. git status Is the change whatyou meant? no → edit again, orgit restore <file> git diff (read it) Does it belong in thenext commit? no → leave it unstaged,or git stash it git add <file> → git diff --staged Complete, and aboutone thing? no → stage more files,or part with add -p git commit -m "type: … (#n)"then pull, then push when ready yes ↓yes ↓yes ↓yes ↓ nononoyes Every "no" leads back into the same loop; nothing is lost except with git restore.
After any edit: status, then diff, then stage, then check the staged diff, then commit. Every "no" answer has a safe exit.

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 status shows it as untracked; git add starts tracking it; the rest is identical.
  • A deleted file: shows as deleted; git add <file> or git 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 -p to stage one at a time (lesson 5.3).

Check yourself

1. You ran git diff and the change is right, but it belongs to a different piece of work than the commit you are building. What do you do?
2. After git diff --staged you see two unrelated changes in the same file. Which command lets you commit only one?

Key terms

Restore Staged Commit