Lesson 4 / الدرس 4

The three places a file can be / المواضع الثلاثة التي قد يكون فيها الملف

Almost every confusing thing about git comes from not knowing this. A file is in your folder, or staged, or committed — and each command moves it between exactly two of those.

كل ما يحيّر في git تقريبًا يأتي من عدم معرفة هذا. فالملف إما في مجلدك، أو مجهَّز، أو مودَع — وكل أمر ينقله بين اثنين منها بالضبط.

This lesson has no new commands in it. It is a picture, and it is the single most useful thing to hold in your head while learning git — because every command in the next six lessons is an arrow between two of these three places.

  working directory        staging area          repository
  (your actual files)        (what is next)        (the history)

         │  ──────  git add  ──────>  │                     │
         │                            │ ── git commit ──>   │
         │  <────────────  git restore / git checkout ───   │
Three places, and the commands between them. Run the demonstration to see what each one is for — in particular the middle one, which is the part every other version control system leaves out and which confuses everyone at first.

Why the middle one exists

The staging area is the part people ask about. Why not just commit the files? Because your working directory rarely contains exactly one coherent change. You fixed a bug, and while you were there you renamed a variable and added a note to a different file. Staging lets you commit the bug fix on its own, with a message about the bug, and commit the rest separately.

Git calls a fileWhengit status colour
untrackedIt is new and git has never seen itRed, under "untracked files"
modifiedIt is tracked and you changed itRed, under "not staged"
stagedYou ran git add on itGreen, under "to be committed"
committedIt is in the history and unchanged sinceNot listed at all

The last row is why a clean git status is short. Anything git does not mention is already safely in the history, exactly as it is on disk.

The same file in two places at once

One consequence surprises everybody. Stage a file, then edit it again before committing, and git lists it twice — once as staged and once as modified. That is not a bug: the staged version is the one you chose, and the file on disk has since moved on. Committing now saves the staged version, not what is on disk.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What is the staging area for?

2. You stage a file, then edit it again, then commit. What is saved?

3. Which of the three places is permanent?

Your task / مهمتك

Draw the three places as a page, in your own words: what each one is, what it is for, and which command moves a file into it. Then explain the staging area to someone who thinks it is pointless — give a concrete example of a working directory containing two unrelated changes and say what you would commit first and why.

ارسم المواضع الثلاثة في صفحة بكلماتك: ما كل واحد، وما وظيفته، وأي أمر ينقل ملفًا إليه. ثم اشرح منطقة التجهيز لمن يظنها بلا فائدة — بمثال محدد لمجلد عامل فيه تغييران غير مترابطين وقل ما الذي ستودعه أولًا ولماذا.

  • All three places, in your own words المواضع الثلاثة كلها بكلماتك
  • The command that moves a file into each الأمر الذي ينقل ملفًا إلى كل واحد
  • A concrete two-change example مثال محدد بتغييرين
  • Which is permanent, said explicitly أيها دائم، مقولًا صراحةً
How do you want to submit? / كيف تريد التسليم؟