Check if directory is git repository (without having to cd into it)

前端 未结 4 1353
庸人自扰
庸人自扰 2021-02-18 12:51

What\'s a shell command I can use to, using the full directory path, determine whether or not a given directory is a git repository? Specifically, I\'d like to be able to do thi

相关标签:
4条回答
  • 2021-02-18 13:31

    Any directory in the system could be a git working copy. You can use an directory as if it contained a .git subdirectory by setting the GIT_DIR and GIT_WORK_TREE environment variables to point at an actual .git directory and the root of your working copy, or use the --git-dir and --work-tree options instead. See the git man page for more details.

    0 讨论(0)
  • 2021-02-18 13:37

    Adding to @Walk's comments, git status will throw fatal message if its not a repo. echo $? will help to capture of its an ERROR or not. If its a git repo then echo $? will be 0 else 128 will be the value

    0 讨论(0)
  • 2021-02-18 13:39

    We can also try git status command and if the output is like :

    fatal: Not a git repository (or any of the parent directories): .git

    Then, the directory is not a git repository.

    0 讨论(0)
  • 2021-02-18 13:44

    Use git -C <path> rev-parse. It will return 0 if the directory at <path> is a git repository and an error code otherwise.

    0 讨论(0)
提交回复
热议问题