git.day/8 • See all at git.day
Usually Git can handle moving files around in the repo, keeping track of renames and moves. However Git doesn't actually follow moves around; as discussed in Git of the day #7: git add --intent-to-add, commits are not diffs from the previous commit, but rather snapshots of state. Sometimes Git needs a bit of a hand setting the index to produce the index for the new state.
The most common case where Git needs a hand is after renaming a file changing its case. Let's rename a file changing its case only1 and see what happens:
$ ls
Foo
$ mv Foo foo
$ ls
foo
$ git status
On branch master
nothing to commit, working tree clean
This is the problem: the filesystem has changed but Git has not picked up the move. This can be resolved with git mv
in place of the regular mv
.
$ git mv Foo foo
$ git status
On branch master
changes to be committed:
renamed: Foo -> foo
While Git is great at tracking changes to the filesystem, this extra command helps with edge cases such as this, ensuring changes can be added correctly.
- This example is on macOS with an APFS volume. ↩︎