I wanted a simple git command to go up to the "root" of the repository.
I started with a script, but figured that I cannot change active directory of the shell, I had to do a function. Unfortunately, I cannot call it directly with the non-dash form "git root", for instance.
function git-root() { if [ -d .git ]; then return 0 fi A=.. while ! [ -d $A/.git ]; do A="$A/.." done cd $A }
Do you have a better solution? (the function has been written quickly, suggestions are welcome)
--git-dir Show $GIT_DIR if defined else show the path to the . git directory. You can see it in action in this git setup-sh script. If you are using git rev-parse --show-toplevel , make sure it is with Git 2.25+ (Q1 2020).
git folder are known as the Git working tree. The working tree is the set of all files and folders a developer can add, edit, rename and delete during application development. The status command can provide insight into how the Git working tree behaves.
a . git directory at the root of the working tree; a <project>. git directory that is a bare repository (i.e. without its own working tree), that is typically used for exchanging histories with others by pushing into it and fetching from it.
This has been asked before, Is there a way to get the git root directory in one command? Copying @docgnome's answer, he writes
cd $(git rev-parse --show-cdup)
Make an alias if you like:
alias git-root='cd $(git rev-parse --show-cdup)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With