DEV Community

Cover image for #gitPanic - Files
Abbey Perini
Abbey Perini

Posted on • Updated on

#gitPanic - Files

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.

  1. git commit
  2. git add
  3. git rm
  4. git mv
  5. Configure Case Sensitivity
  6. git ls-files
  7. git ls-tree

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.

Black man tapping the side of his head to indicate a smart idea captioned "Don't need a version control system if you have ctrl + v"

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

and move a file

git mv <fileName> <newDirectory>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

will work. To change the git settings to be case-sensitive, run

git config core.ignorecase false
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Conclusion

These commands really show just how much information git is storing about your repo and the changes you make!

Top comments (0)

Some comments have been hidden by the post's author - find out more