#gitPanic - Files

#gitPanic - Files

ยท

3 min read

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. git ls-files
  6. 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

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>

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!

ย