Imagine there are fileA fileB fileC, and you modify them. Then commit.
git add .
git commit -m "update"
All files are commited and tracked by git.
Imagine fileD is created by you or some toolchain, and you modify fileD and fileA. Then commit.
git add .
git commit -m "update"
fileD gets tracked by git (git add .
), and fileA and fileD are tracked by git.
With regarding to "So why does git commit -am "x"
work ... but .... git add .
git commit -m "x"
does not work?"
Ans:
IT IS BECAUSE you did something like this:
Imagine fileE is created by you or some toolchain (let's say it is fileE version1), and you staged it
git add .
and then you modify fileE (let's say it becomes fileE version2). ^ Then commit.
git commit -m "update"
As a result, only version1 of fileE is committed. Although it confuses the user, it is the expected behaviour of git: git doesn't know about fileE version2. ---- for the uncommited fileE, you staged it too early as you forget to let git know about fileE version2 with doing a git add fileE
(at ^ mark of moment) after a newer version of fileE, before committing.
So just remember to git add everything of its newest version before commit. (add new version, commit new version)
-
LET'S ROLL BACK the whole story before fileE is created, and do it again so that you don't miss the fileE version2.
Imagine fileE is created by you or some toolchain (let's say it is fileE version1), and you modify fileE (let's say it becomes fileE version2).
Then commit.
git add .
git commit -m "update"
As a result, version2 of fileE is committed.
If you want the version1 fileE to be in a commit in your commit history, simply just commit twice: once for its old version, once for its new version (aka. once before its being modified to version2, once for its version2)
Tip:
Always follow this sequence: (add new version, commit new version)
(file modification)
+ git add .
+ git commit -m "update"
,
(file modification again)
+ git add .
+ git commit -m "update"
,
and you won't run into this problem of missing any changes.
And you don't even need the "convenience" provide by -am
. Because using -am
is equivalent to asking git to be smart and be lenient about your forgetting git add .
With a clear mind to follow the intuitive sequence, you don't need lenience at this level. Sometimes, unnecessary lenience bring more brain load than benefits.