Check if current directory is a Git repository
GitZshGit Problem Overview
I am writing a series of scripts for Git management in zsh.
How do I check if the current directory is a Git repository? (When I'm not in a Git repo, I don't want to execute a bunch of commands and get a bunch of fatal: Not a git repository
responses).
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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
Solution 3 - Git
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.
Solution 4 - Git
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
Solution 5 - Git
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.
-
As @go2null noted, this will not work in a bare repo. If you want to work with a bare repo for whatever reason, you can just check for
git rev-parse
succeeding, ignoring its output.- I don't consider this a drawback because the above line is indended for scripting, and virtually all
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.
- I don't consider this a drawback because the above line is indended for scripting, and virtually all
Solution 6 - Git
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
Solution 7 - Git
Another solution is to check for the command's exit code.
git rev-parse 2> /dev/null; [ $? == 0 ] && echo 1
This will print 1 if you're in a git repository folder.
Solution 8 - Git
This answer provides a sample POSIX shell function and a usage example to complement @jabbie's answer.
is_inside_git_repo() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1
}
git
returns errorlevel 0
if it is inside a git repository, else it returns errorlevel 128
. (It also returns true
or false
if it is inside a git repository.)
Usage example
for repo in *; do
# skip files
[ -d "$repo" ] || continue
# run commands in subshell so each loop starts in the current dir
(
cd "$repo"
# skip plain directories
is_inside_git_repo || continue
printf '== %s ==\n' "$repo"
git remote update --prune 'origin' # example command
# other commands here
)
done
Solution 9 - Git
Why not using exit codes? If a git repository exists in the current directory, then git branch
and git tag
commands return exit code of 0; otherwise, a non-zero exit code will be returned. This way, you can determine if a git repository exist or not. Simply, you can run:
git tag > /dev/null 2>&1
Advantage: Portable. It works for both bare and non-bare repositories, and in sh, zsh and bash.
Explanation
git tag
: Getting tags of the repository to determine if exists or not.> /dev/null 2>&1
: Preventing from printing anything, including normal and error outputs.
check-git-repo
TLDR (Really?!): As an example, you can create a file named check-git-repo
with the following contents, make it executable and run it:
#!/bin/sh
if git tag > /dev/null 2>&1; then
echo "Repository exists!";
else
echo "No repository here.";
fi
Solution 10 - Git
This works for me. You still get the errors but they're easy enough to suppress. it also works from within subfolders!
git status >/dev/null 2>&1 && echo Hello World!
You can put this in an if then statement if you need to conditionally do more.
Solution 11 - Git
check if git repo
if [ $(git rev-parse --is-inside-work-tree) = true ]; then
echo "yes, is a git repo"
git pull
else
echo "no, is not a git repo"
git clone url --depth 1
fi
Solution 12 - Git
if ! [[ $(pwd) = *.git/* || $(pwd) = *.git ]]; then
if type -P git >/dev/null; then
! git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
printf '\n%s\n\n' "GIT repository detected." && git status
}
fi
fi
Thank you ivan_pozdeev, Now I have a test if inside the .git directory the code will not run so no errors printed out or false exit status.
The "! [[ $(pwd) = .git/ || $(pwd) = *.git ]]" tests if you're not inside a .git repo then it will run the git command. The builtin type command is use to check if you have git installed or it is within your PATH. see help type
Solution 13 - Git
##Current branch
echo $(git branch --show-current 2> /dev/null && echo '')
echo $(git branch --show-current 2> /dev/null)
##OR
GIT_DIR=$(git rev-parse --git-dir 2> /dev/null)
Solution 14 - Git
open that directory in terminal in linux or cmd on windows and enter git status
which should be enough in 2021 to tell you that youre in a git repo and that you have x
number of branches with x
number of commits.