#gitPanic - Files
Abbey Perini
Posted on November 22, 2022
I only learned about these commands because I was complaining about git not reliably tracking changes to files I made in the GUI (Finder, VS Code). Turns out git will always track file name changes, moving files, and deleting files ...if you tell it about them.
This blog assumes you have a basic understanding of git and command line or have read git 101 and HEAD.
git commit
If you don't want to use git's commands to delete or modify files, just tell it to stage those changes when you commit using git commit -a
or git commit --all
. You'll still have to add new files that git hasn't tracked before.
git add
Using git add .
will add new files and deleted files, but won't stage file name changes. You can stage all changes to all tracked files before committing using git add -u
or git add --update
. You can stage all changes required to match your current working directory, including new files, with git add -A
. Once again, A stands for all. You can also pass a file path after this option to only include all changes for that file.
git rm
Running
git rm -rf
works like sudo rm -rf
, but git tracks the changes.
git mv
Using git mv
tells git to track the changes while you update the file path. You can use it to
rename a file
git mv <oldName> <newName>
and move a file
git mv <fileName> <newDirectory>
Configure Case Sensitivity
By default, git is case-insensitive when it comes to file and directory names. With the default settings, these commands won't work:
git mv settings.js Settings.js
git mv components/settings.js Components/Settings.js
It can't tell the difference between the old name and the new name.
It will, however, rename the file if you're changing case and another part of the file name. For instance,
git mv top-bar.js TopBar.js
will work. To change the git settings to be case-sensitive, run
git config core.ignorecase false
and then you can use git mv
to change file and directory name case all you like.
git ls-files
Like ls
, git ls-files
will list all the files in your directory. The difference is git ls-files
takes into account the remote repository, local repository, index, and working directory. For example, running
git ls-files --modified
will show you all the staged files that are different from the last commit. This is just the beginning. There are many more options that will show you all kinds of information about your repo.
git ls-tree
Similar to git ls-files
, git ls-tree
will list all of the data types in your repository. There are, again, many options for formatting.
You have to pass a working directory to ls-tree like
git ls-tree <ref>
In this case, the ref has to be to a tree-ish object (aka directory) like HEAD or a branch name. You can also specify a file path in a tree-ish object like
git ls-tree main:README
Conclusion
These commands really show just how much information git is storing about your repo and the changes you make!
Posted on November 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.