Skip to content

Instantly share code, notes, and snippets.

@hilbix
Last active March 10, 2019 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hilbix/7724772 to your computer and use it in GitHub Desktop.
Save hilbix/7724772 to your computer and use it in GitHub Desktop.
`git exec` to exec command within GIT directory. `git top` to exec on submodule top level directory (somewhat).
git config --global alias.exec '!exec '
git config --global alias.top '!f() { GIT_TOP="${GIT_DIR%%/.git/modules/*}"; [ ".$GIT_TOP" != ".$GIT_DIR" ] && cd "$GIT_TOP"; exec "$@"; }; f'
# THIS IS PUBLIC DOMAIN.
#
# `git exec` executes something in the current GIT module's base directory. `git exec pwd` gives this base.
# Stolen from http://stackoverflow.com/questions/957928/is-there-a-way-to-get-the-git-root-directory-in-one-command/957978#comment9747528_957978
#
# `git top` is like `git exec`, but executes in the topmost GIT directory, even from within a GIT submodule.
# - needs /bin/bash as your shell
# - needs `GIT_DIR` is within the worktree
# - needs `.git`-Files for submodules like in recent GIT
# - does not work for bare repositories (of course)
# - softlinks are expanded as if you run `cd $(readlink -e .)`
@hilbix
Copy link
Author

hilbix commented Mar 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment