fog37 said:
- Initially, the entire file cabinet has only 1 cabinet (branch 1). But we then create 3 more cabinets, i.e. three new branches (branch 2,3,4) and end up with the grey file cabinet in the image above.
This isn't complete. What files were in your working directory when you created the other branches?
For example, suppose you are on branch 1, and your working directory contains one file, file1.txt, which is committed. That means branch 1 has that one file in it.
Now you create branch 2. Branch 2 now has the same set of files in it as branch 1. So branch 2 also has that one file in it, and your working directory will look the same if you switch to branch 2.
But now suppose you create another file while you are on branch 2, file2.txt, and commit it. Now branch 2 contains two files, but branch 1 still contains only one; so if you switch back to branch 1, file2.txt will no longer be in your working directory.
If you then
merge branch 2 into branch 1, branch 1 will be updated to contain whatever branch 2 contains. So after the merge, file2.txt will be in branch 1 as well as branch 2.
fog37 said:
- We can only open one cabinet at a time (i.e. we can only be on a branch at a time).
Yes.
fog37 said:
- At the very beginning, we open cabinet 1 (branch 1). All other cabinets/branches are closed. We have files A and B inside cabinet 1. These two files will be placed by Git (clerk) on our desk (working directory).
Yes.
fog37 said:
- We then open cabinet 2 (branch 2). Cabinet 1 closes automatically. What files are inside cabinet 2?
It depends on what files were in your working directory when branch 2 was created. See above.
fog37 said:
- Well, magically, the clerk (Git) has placed inside cabinet 2 two files which are identical copies of file A and B.
Not if you just
switch to branch 2. If you
create branch 2 when you are on branch 1 with those files,
then branch 2 will also contain them. But if you just
switch to branch 2, Git will not update the files in the cabinet for branch 2; it will just clear your desk and then put on your desk whatever files were
already in the cabinet for branch 2.
fog37 said:
- Our desk (working directory) has been cleared and now has those two exact copies of file A and B.
No. See above.
fog37 said:
- While cabinet 2 (branch 2) is open, I decide to create a new file C. File C ends up both on my desk
On your desk, yes. But...
fog37 said:
- and inside cabinet 2 (branch 2) only.
Not when you create file C. Only when you
commit file C while on branch 2. Committing is what takes the files on your desk (more precisely, the ones that are staged for commit) and updates the files in the cabinet under your current branch from them.
fog37 said:
- My desk (working directory) reflects what is inside the currently open cabinet.
Only if everything in your working directory is committed.
fog37 said:
- We close cabinet 2 (branch 2) and reopen cabinet 1 (branch 1). File C is not inside cabinet 1 and neither on my desk (which mirrors what is inside the open cabinet). This shows that the working directory is updated and only shows the files on the current branch (Currently open cabinet). In general, cabinets contain exact copies of the same files, different copies of the same files, or even new different files...My desk, i.e. the working directory, reflects only what is inside the cabinet that is currently open.
More or less. See above.
fog37 said:
- We reopen cabinet 2 (which closes cabinet/branch 1). We have files A,B,C. We modify file A. File A is different from file A stored in cabinet/branch 1, correct?
Yes.
fog37 said:
The analogy skips to include the staging area and commit area. How would you modify the analogy to include those concepts? The desk is the working directory. What about the commit area and the staging area?
There is no "commit area"; committing means updating the files in the cabinet (see above), which you already have in your analogy.
To add the staging area, imagine a special tray at one corner of your desk marked "staging". You have a bunch of files on your desk, all matching what is in the currently open cabinet (since everything is committed). Then you change a file, say file A. Now your desk is out of sync with what's in the cabinet.
If you decide you want to store the change you made in file A in the cabinet, you "stage" file A by putting it in the tray marked "staging". The git command that does this is
git add
. Then
git commit
updates the currently open cabinet using whatever is in the "staging" tray, and puts the files in the tray back on your desk. Assuming you haven't changed any other files on your desk, your desk is now in sync with the currently open file cabinet.