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).
Copied from the bash completion file the following is a naive way to do it
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# 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 or zsh
[ -d .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
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
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
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.
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-parsesucceeding, ignoring its output.- I don't consider this a drawback because the above line is indended for scripting, and virtually all
gitcommands 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
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.
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
# 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
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 && [ $? -eq 0 ]
Advantage: Flexibe. 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.[ $? -eq 0 ]: Check if the previous command returned with exit code 0 or not. As you may know, every non-zero exit means something bad happened.$?gets the exit code of the previous command, and[,-eqand]perform the comparison.
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 && [ $? -eq 0 ]; then
echo "Repository exists!";
else
echo "No repository here.";
fi
! git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
printf '%s\n\n' "GIT repository detected." && git status
}
The ! negates so even if you run this in a directory that is not a git repo it will not give you some fatal errors
The >/dev/null 2>&1 sends the messages to /dev/null since you're just after the exit status. The {} are for command groupings so all commands after the || will run if the git rev-parse succeeded since we use a ! which negated the exit status of git rev-parse. The printf is just to print some message and git status to print the status of the repo.
Wrap it in a function or put it in a script. Hope this helps
来源:https://stackoverflow.com/questions/2180270/check-if-current-directory-is-a-git-repository