So how can you tell if a directory is within a git repository? Exit code of 0 means it's a git repository. Any other code (e.g., 128 ) means it's not.
git directory was added to the git_basics directory. Conceptually, you can think about it as turning that directory into a git repository. After you initialize a directory as a git repository, you can start issuing other git commands.
git init Existing Folder For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.
git folder in your "local repos" a.k.a. "working folder/directory". Folder/directory here is a folder that can contain other folders/directories - as well as files - indeed it will always contain a . git folder, otherwise it would not be a repository.
Copied from the bash completion file, the following is a naive way to do it
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
if [ -d .git ]; then
echo .git;
else
git rev-parse --git-dir 2> /dev/null;
fi;
You could either wrap that in a function or use it in a script.
Condensed into a one line condition suitable for bash and zsh
[ -d .git ] && echo .git || git rev-parse --git-dir > /dev/null 2>&1
You can use:
git rev-parse --is-inside-work-tree
Which will print 'true' if you are in a git repos working tree.
Note that it still returns output to STDERR if you are outside of a git repo (and does not print 'false').
Taken from this answer: https://stackoverflow.com/a/2044714/12983
Use git rev-parse --git-dir
if git rev-parse --git-dir > /dev/null 2>&1; then
: # This is a valid git repository (but the current working
# directory may not be the top level.
# Check the output of the git rev-parse command if you care)
else
: # this is not a git repository
fi
edit: git-rev-parse
now (as of 1.7.0) supports --show-toplevel
, so you could do if test "$(pwd)" = "$(git rev-parse --show-toplevel)"
to determine if the current directory is the top-level dir.
Or you could do this:
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [ "$inside_git_repo" ]; then
echo "inside git repo"
else
echo "not in git repo"
fi
Based on @Alex Cory's answer:
[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]
doesn't contain any redundant operations and works in -e
mode.
git rev-parse
succeeding, ignoring its output.
git
commands are only valid inside a worktree. So for scripting purposes, you're most likely interested in being not just inside a "git repo" but inside a worktree.Not sure if there is a publicly accessible/documented way to do this (there are some internal git functions which you can use/abuse in the git source itself)
You could do something like;
if ! git ls-files >& /dev/null; then
echo "not in git"
fi
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