# [[Git Cheat Sheet|Git Cheat Sheet]]
Various recipes that I have collected over the years.
>[!important]
A lot of this have been copied verbatim from man pages or other sources. I have simply collected them here for my own quick reference. I make no claims to originality. Whenever possible, I have tried to link to the original source.
## Find the absolute path to the top level directory for a git repo from a subdirectory
```bash
git rev-parse --show-toplevel
```
This returns the absolute path, which is very useful in shell scripts/functions to use as the base for paths in a repository, accounting for machine to machine differences.
```shell-session
$ git rev-parse --show-toplevel
/Users/sbhattacharya/dev/sandipb/kb.sandipb.net/git
```
## Find current directory path relative to the git root
[Stack overflow](https://stackoverflow.com/a/39923104)
```bash
git rev-parse --show-prefix
```
This is another useful command to use in shell scripts/functions. This lets you get the relative path of the current directory to the git root.
## Find all files added/modified/deleted etc since a version in the past
([Reference](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-code--diff-filterACDMRTUXBcode "git-diff man page"))
```bash
git diff --name-only --diff-filter=A v0.0.6
```
- `--name-only`: This option ensures that only the filenames are shown in the output.
- `--diff-filter=A`: This option filters the output to only show added files.
The possible values for the `--diff-filter` option are:
- `A`: Added files
- `C`: Copied files
- `D`: Deleted files
- `M`: Modified files
- `R`: Renamed files
- `T`: Files whose type has changed (e.g., regular file, symlink, submodule, etc.)
- `U`: Unmerged files
- `X`: Unknown files
- `B`: Files that have had their pairing broken
Additional notes:
- You can use any combination of these filter characters, including none. For example, `git diff --diff-filter=AC` will show only added and copied files.
- If you add `*` (All-or-none) to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected. e.g. `git diff --diff-filter=M*` will show all changes (added, copied, deleted, modified, etc.) if there is at least one modifiedfile. If there are no modified files, the output will be empty.
- Additionally, these upper-case letters can be downcased to exclude certain types of changes. For instance, `git diff --diff-filter=ad` will exclude added and deleted paths from the output.