Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: a quick command to go to root of the working tree

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)

like image 484
elmarco Avatar asked Oct 15 '09 10:10

elmarco


People also ask

How do I find my git root directory?

--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).

What is the working tree git?

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.

What is root in git?

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.


1 Answers

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)' 
like image 84
Peter Avatar answered Sep 20 '22 15:09

Peter