GIT - 4.12 Manage files outside of GIT
This lesson shows what happens when you rename, move or delete files using only the operating system (without going through Git), and how to reconcile those changes with the index afterwards. The starting point is a clean repository on master containing coucou.html and venus.html.
First we create a brand new file with the shell command touch uranus.html. Then we rename Venus by changing both its name and its extension at the OS level:
mv venus.html venus.txt
Running git status shows two interesting things. The new file uranus.html appears as untracked, which is expected. But the rename is not recognized as a rename: Git sees it as a deletion of venus.html plus an untracked venus.txt. That is the price of bypassing git mv.
Catching up with git add -u and git add -A
git add -u(update) stages only modifications and deletions of already tracked files. Untracked new files are ignored.git add -A(all) covers everything — modifications, deletions, and new untracked files.
With git add -A, Git correctly detects that venus.html was renamed to venus.txt, and also stages the new file uranus.html. We then commit everything together:
git commit -m "rename and add"
If we later decide we no longer want uranus.html, we can also delete it at the OS level using the shell rm command:
rm uranus.html
git add -u
git commit -m "delete uranus.html"
git add -u picks up the deletion and stages it; the commit then records the removal. The key takeaway is that Git does notice OS-level operations, but it sometimes interprets them differently (renames as delete+add) and you must remember to refresh the index with -u or -A before committing.