diff --git a/README.md b/README.md index d05fb1c..a2670e1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1718 TILs and counting..._ +_1719 TILs and counting..._ See some of the other learning resources I work on: @@ -349,6 +349,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Count Number Of Commits On A Branch](git/count-number-of-commits-on-a-branch.md) - [Create A New Branch With Git Switch](git/create-a-new-branch-with-git-switch.md) - [Delete All Untracked Files](git/delete-all-untracked-files.md) +- [Determine Absolute Path Of Top-Level Project Directory](git/determine-absolute-path-of-top-level-project-directory.md) - [Determine The Hash Id For A Blob](git/determine-the-hash-id-for-a-blob.md) - [Diffing With Patience](git/diffing-with-patience.md) - [Dropping Commits With Git Rebase](git/dropping-commits-with-git-rebase.md) diff --git a/git/determine-absolute-path-of-top-level-project-directory.md b/git/determine-absolute-path-of-top-level-project-directory.md new file mode 100644 index 0000000..efb4b66 --- /dev/null +++ b/git/determine-absolute-path-of-top-level-project-directory.md @@ -0,0 +1,39 @@ +# Determine Absolute Path Of Top-Level Project Directory + +The `git rev-parse` command is a git plumbing command for parsing different +kinds of things in git into a canonical form that can be used in a deterministic +way by scripts. I would typically think of using it to work with branch names, +tags, and other kinds of refs. + +There is a handy, sorta off-label use for it in determining the absolute path of +the root directory for the current git repository. Use the `--show-toplevel` +flag with no other arguments. + +```bash +❯ git rev-parse --show-toplevel +/Users/lastword/dev/jbranchaud/til +``` + +Here, I am in the local copy of [my TIL repo](https://github.com/jbranchaud/til). This command gives me the absolute +path of the top-level directory where that `.git` directory resides. + +This is useful for scripts that need to orient themselves to the current +project's top-level directory regardless of what directory they are being +executed from. This is useful for things like a git hook script or monorepos +with scripts located in a specific sub-project directory. + +Also worth mentioning is the `--show-superproject-working-tree` flag. In my TIL +repo, I have a private repository included as a submodule. Within that directory +`--show-toplevel` will produce the absolute path to the submodule. If I instead +want the absolute path of the _super project_ (in this case TIL), then I can use +this other flag. + +```bash +❯ git rev-parse --show-toplevel +/Users/lastword/dev/jbranchaud/til/notes + +❯ git rev-parse --show-superproject-working-tree +/Users/lastword/dev/jbranchaud/til +``` + +See `man git-rev-parse` for more details.