简体   繁体   English

git bash如何显示当前目录和分支

[英]how to display the current directory and branch in git bash

My git bash terminal how do i make my git bash terminal look like the below one.我的 git bash 终端如何使我的 git ZD574D4BB40C848617691A694A9Z9 终端看起来像下面的一个。 i want to display the current working directory and the current branch.我想显示当前工作目录和当前分支。 my terminal use to look like the below one.我的终端使用如下所示。 but for some reason it is only showing "->".但由于某种原因,它只显示“->”。 My friends git bash terminal please help me.我的朋友git bash终端请帮帮我。 i searched a lot for this question.我搜索了很多这个问题。 i didn't find any answers.我没有找到任何答案。

Try this:尝试这个:

Paste in ~/.profile or in ~/.bash_profile :粘贴到~/.profile~/.bash_profile

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


#update your prompt string
export PS1="[\u@\h \W]\\$\\$(git_branch)"

then source ~/.profile or source ~/.bash_profile然后source ~/.profilesource ~/.bash_profile

I'm on Windows and wanted to use git within MSYS2, not as a separate git bash terminal (which is what your friends seems to have).我在 Windows 上,想在 MSYS2 中使用 git,而不是作为单独的 git bash 终端(这就是你的朋友似乎拥有的)。 I downloaded Git for Windows and copied the following code from C:\\Program Files\\Git\\etc\\profile.d\\git-prompt.sh into my bashrc file:我下载了适用于 Windows 的 Git 并将以下代码从 C:\\Program Files\\Git\\etc\\profile.d\\git-prompt.sh 复制到我的 bashrc 文件中:

    if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[32m\]'       # change to green
    PS1="$PS1"'\u@\h '             # user@host<space>
    PS1="$PS1"'\[\033[35m\]'       # change to purple
    PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
    PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
    PS1="$PS1"'\w'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        if test -f "$COMPLETION_PATH/git-prompt.sh"
        then
            . "$COMPLETION_PATH/git-completion.bash"
            . "$COMPLETION_PATH/git-prompt.sh"
            PS1="$PS1"'\[\033[36m\]'  # change color to cyan
            PS1="$PS1"'`__git_ps1`'   # bash function
        fi
    fi
    PS1="$PS1"'\[\033[0m\]'        # change color
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'$ '                 # prompt: always $
fi

MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

To do this, open an instance of MSYS2, open bashrc using any editor, for example with nano type nano ~/.bashrc , scroll to the bottom of the file, and paste the above code in. New MSYS2 instances should look like your friend's!为此,打开一个 MSYS2 实例,使用任何编辑器打开 bashrc,例如使用 nano 类型nano ~/.bashrc ,滚动到文件底部,然后粘贴上面的代码。新的 MSYS2 实例应该看起来像你朋友的!

This is what my prompt looks like:这是我的提示的样子:

在此处输入图像描述

Instead of showing the full path, it just shows the current branch and directory.它不显示完整路径,而只显示当前分支和目录。 It's designed with the file git-prompt.sh which contains:它是用文件git-prompt.sh设计的,其中包含:

PS1='\[\033]0;$TITLEPREFIX:\W\007\]' # set window title
PS1="$PS1"'\n'                       # new line
PS1="$PS1"'\[\033[35m\]'             # change to purple
PS1="$PS1"'git'                      # git user
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[36m\]'   # change color to cyan
        PS1="$PS1"'`__git_ps1` '   # bash function
    fi
fi
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'@\W'                # current working directory
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'> '                 # prompt: default $
PS1="$PS1"'\[\033[0m\]'        # change color

To edit the git-prompt.sh file, first go to your user profile folder.要编辑git-prompt.sh文件,首先将 go 到您的用户配置文件文件夹。 Example:例子:

C:\Users\myProfile

Then go to the folder \.config\git Example:然后 go 到文件夹\.config\git示例:

C:\Users\myProfile\.config\git

Then create or edit the git-prompt.sh file, and paste in the code block above.然后创建或编辑git-prompt.sh文件,并粘贴到上面的代码块中。 The full path of git-prompt.sh should be: git-prompt.sh的完整路径应该是:

C:\Users\myProfile\.config\git\git-prompt.sh

For reference, the default git-prompt.sh file is below:作为参考,默认的git-prompt.sh文件如下:

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[32m\]'       # change to green
    PS1="$PS1"'\u@\h '             # user@host<space>
    PS1="$PS1"'\[\033[35m\]'       # change to purple
    PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
    PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
    PS1="$PS1"'\w'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        if test -f "$COMPLETION_PATH/git-prompt.sh"
        then
            . "$COMPLETION_PATH/git-completion.bash"
            . "$COMPLETION_PATH/git-prompt.sh"
            PS1="$PS1"'\[\033[36m\]'  # change color to cyan
            PS1="$PS1"'`__git_ps1`'   # bash function
        fi
    fi
    PS1="$PS1"'\[\033[0m\]'        # change color
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'$ '                 # prompt: always $
fi

MSYS2_PS1="$PS1"     # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

The default git-prompt.sh file may be found in any of the following folders:默认的git-prompt.sh文件可以在以下任何文件夹中找到:

C:\Users\myProfile\AppData\Local\Programs\Git\etc\profile.d\git-prompt.sh

C:\Program Files\Git\etc\profile.d\git-prompt.sh

Those can be edited directly instead of making a copy in your user profile.这些可以直接编辑,而不是在您的用户个人资料中制作副本。 Editing those may change the prompt for all user profiles in your computer.编辑这些可能会更改计算机中所有用户配置文件的提示。

Git bash 与任何其他 bash 一样,因此只需运行pwd (当前工作目录),对于分支使用git branch -a

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

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