From 2effb7d77b557cf2381559432006cff40731ac97 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 16 Mar 2021 22:33:07 -0500 Subject: [PATCH] Add Run A Git Command From Outside The Repo as a git til --- README.md | 3 ++- ...run-a-git-command-from-outside-the-repo.md | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 git/run-a-git-command-from-outside-the-repo.md diff --git a/README.md b/README.md index 5b12af6..5a38535 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1088 TILs and counting..._ +_1089 TILs and counting..._ --- @@ -272,6 +272,7 @@ _1088 TILs and counting..._ - [Renaming A Branch](git/renaming-a-branch.md) - [Resetting A Reset](git/resetting-a-reset.md) - [Resolve A Merge Conflict From Stash Pop](git/resolve-a-merge-conflict-from-stash-pop.md) +- [Run A Git Command From Outside The Repo](git/run-a-git-command-from-outside-the-repo.md) - [Set A Custom Pager For A Specific Command](git/set-a-custom-pager-for-a-specific-command.md) - [Show All Commits For A File Beyond Renaming](git/show-all-commits-for-a-file-beyond-renaming.md) - [Show Changes For Files That Match A Pattern](git/show-changes-for-files-that-match-a-pattern.md) diff --git a/git/run-a-git-command-from-outside-the-repo.md b/git/run-a-git-command-from-outside-the-repo.md new file mode 100644 index 0000000..49e2476 --- /dev/null +++ b/git/run-a-git-command-from-outside-the-repo.md @@ -0,0 +1,20 @@ +# Run A Git Command From Outside The Repo + +Generally you run a git command from somewhere within the parent folder where +the `.git` directory lives. Git recognizes the `.git` directory in that parent +directory and runs your command against it. + +You can run a command against a given git repository without being within the +parent directory. This can be handy for scripting as well as for one-off +commands when you don't want to `cd` to the directory. To do this, you need to +tell Git where to find the `.git` directory. You do this with the `-C` flag. + +For instance, from anywhere on my machine, I can view a log of this TIL +repository with the following: + +```bash +$ git -C ~/code/til log +``` + +Notice that the `-C` flag and its argument are positioned directly after `git`. +The command (`log`) should be positioned after that.