简体   繁体   English

如何使用bash脚本检查目录中是否存在git存储库

[英]How to check for the presence of a git repository in a directory using bash script

I am trying to work with git on my project. 我正在尝试在项目中使用git。

I want to set up the terminal such that whenever I cd into a directory that contains a git project, the terminal should indicate which git branch I am currently on. 我想设置终端,以便每当我cd进入包含git项目的目录时,终端都应指示我当前在哪个git分支上。

If there is a git project, the terminal show only the name of the branch, for example 如果存在git项目,则终端仅显示分支名称,例如

(master) $

otherwise it should show the current directory path.ie 否则,它应该显示当前目录path.ie

username@machinename:path/to/directory $

I found a similar question answered here but i don't know how to modify the answer to suit my need, because am not good with bash scripting. 我在这里找到了一个类似的问题但是我不知道如何修改答案以适应我的需要,因为bash脚本编写不好。 Any suggestion will be highly appreciated. 任何建议将不胜感激。

Have a look at this project https://github.com/jimeh/git-aware-prompt , it should solve your whole problem and when not, you can change it to meet your needs. 看看这个项目https://github.com/jimeh/git-aware-prompt ,它应该可以解决您的整个问题,如果没有,您可以更改它以满足您的需求。 Main logic is in prompt.sh. 主要逻辑在hint.sh中。

To find current git branch name in directory, you can always run 要在目录中找到当前的git分支名称,您可以始终运行

git rev-parse --abbrev-ref HEAD

It will return branch name, HEAD (when detached) or nothing when directory is not par of git repository. 当目录不是git仓库的参数时,它将返回分支名称,HEAD(分离时)或什么都不返回。 If you need only this information, you can update your .bashrc. 如果仅需要此信息,则可以更新.bashrc。 Edit variable PS1, which is bash prompt format. 编辑变量PS1,它是bash提示格式。

PS1='(`git rev-parse --abbrev-ref HEAD 2> /dev/null`) \$ '

This is example how to display branch name anywhere in the prompt. 这是示例如何在提示中的任何位置显示分支名称。 The git script will help you and recognize whether to show branch or directory. git脚本将帮助您并识别是显示分支还是目录。 It will update your PROMPT_COMMAND, which is called every time the bash prompt line is displayed, by checking for git branch name which you can then use in PS1 as a variable. 它将通过检查git分支名称来更新您的PROMPT_COMMAND,每次显示bash提示行时都会调用该名称,然后您可以在PS1中将其用作变量。 You can then update your existing PS1='${debian_chroot:+($debian_chroot)}\\u@\\h:\\w\\$ ' to 然后,您可以将现有的PS1='${debian_chroot:+($debian_chroot)}\\u@\\h:\\w\\$ '

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w \(\$git_branch\)\$ '

The Git package in Linux usually comes with a prompt script that you can use to see the repository status. Linux中的Git软件包通常带有提示脚本 ,您可以使用该脚本查看存储库状态。 To enable it, source the git-prompt.sh script in a shell startup file (eg ~/.bashrc ), then set a custom prompt with the %s parameter. 要启用它,请在外壳启动文件(例如~/.bashrc )中提供git-prompt.sh脚本,然后使用%s参数设置自定义提示。 For example, in ArchLinux you could do: 例如,在ArchLinux中,您可以执行以下操作:

[ -r /usr/share/git/completion/git-prompt.sh ] && . /usr/share/git/completion/git-prompt.sh
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

When changing to a directory of a Git repository, the prompt will change to show the branch name. 当更改为Git存储库的目录时,提示将更改为显示分支名称。 Extra details can be set to be shown by the prompt (see the link above). 可以设置其他详细信息,以通过提示显示(请参阅上面的链接)。

You have many tools which do it. 您有很多工具可以做到这一点。

myzsh for example or just a simplt bash that you add to your bashrc 例如myzsh或只是添加到bashrc中的simplt bash

http://martinvalasek.com/blog/current-git-branch-name-in-command-prompt http://martinvalasek.com/blog/current-git-branch-name-in-command-prompt

example: 例:

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"

PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM