321

我刚刚为 Windows 安装了 Git,很高兴看到它安装了 Bash。

我想以与 Linux 下相同的方式自定义 shell(例如,设置类似llfor的别名ls -l),但我似乎找不到.bashrc或等效的配置文件。

我应该编辑什么?

4

14 回答 14

378

创建一个.bashrc文件,~/.bashrc然后离开。同样对于~/.gitconfig

~通常是您的C:\Users\<your user name>文件夹。在 Git Bash 终端中输入echo ~会告诉您该文件夹是什么。

如果您无法创建文件(例如运行 Windows),请运行以下命令:

copy > ~/.bashrc

该窗口将输出一条错误消息(未找到命令),但该文件将被创建并准备好供您编辑。

于 2011-07-30T14:33:33.120 回答
139

在较新版本的 Git for Windows 中,启动 Bash--login会导致 Bash 无法.bashrc直接读取。相反,它显示为.bash_profile.

如果此文件不存在,请使用以下内容创建它:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

这将导致 Bash 读取.bashrc文件。根据我对这个问题的理解,Windows 版 Git 应该会自动执行此操作。但是,我刚刚安装了 2.5.1 版,但没有安装。

于 2015-09-02T12:24:28.133 回答
8

我必须添加一个用户环境变量,HOME通过C:\Users\<your user name>转到SystemAdvanced System Settings,在System Properties窗口中的Advanced选项卡,Environment Variables ...

然后在我的文件中,C:\Users\<your user name>我创建了文件.bashrc,例如,touch .bashrc并添加了所需的别名。

于 2014-09-30T05:35:44.457 回答
8

我认为这里的问题是如何在 Windows 上找到 .bashrc 文件。

由于您使用的是 Windows,因此您可以简单地使用类似的命令

start .

或者

explorer .

打开带有 Git Bash 安装根目录的窗口,您将在其中找到该.bashrc文件。如果它不存在,您可能需要创建一个。

您可以使用 Notepad++ 等 Windows 工具来编辑文件,而不是在 Bash 窗口中使用 Vim。

于 2016-06-15T00:17:42.767 回答
8

只需notepad ~/.bashrc从 git bash shell 中保存您的文件即可。这应该就是全部了。

注意:请确保您需要重新启动终端以反映更改。

于 2019-07-02T08:09:50.130 回答
7

1) 首先以管理员模式打开 git-bash.exe。(右键单击文件并选择“以管理员身份运行”,或在属性→兼容性→以管理员身份运行此程序中更改设置。)

2)运行cd ~。它会带你到C:/Users/<Your-Username>.

3) 运行vi .bashrc。这将打开您进入编辑器。点击 INSERT 然后开始输入以下信息:

alias ll="ls -la" # this changes the default ll on git bash to see hidden files.
cd "C:\directory\to\your\work\path\"
ll # this shows your your directory before you even type anything.
于 2016-02-05T17:22:06.447 回答
5

请使用以下命令,

cat /etc/bash.bashrc > ~/.bashrc

这将生成一个具有默认值的新 bashrc 文件。请使用vi ~/.bashrc编辑此文件。

于 2020-06-11T04:18:49.283 回答
3

如果您有适用于 Windows 2.21.0 或更高版本的 Git(在撰写本文时),则应在您的主目录中编辑 .bash_profile。

您可以将 .bash_profile 直接指向 .bashrc,但如果您的 .bash_profile 出现问题,那么您的 .bashrc 为何再次无法正常工作就不清楚了。

我把我所有的别名和其他环境的东西放在 .bash_profile 中,我还添加了这一行:

echo "Sourcing ~/.bash_profile - this version of Git Bash doesn't use .bashrc"

然后,在 .bashrc 我有

echo "This version of Git Bash doesn't use .bashrc. Use .bash_profile instead"

(基于@harsel 的回复。我会发表评论,但我还没有积分。)

于 2020-09-10T16:00:21.993 回答
2

不需要创建新文件,它已经存在了!

/etc/bash.bashrc
于 2019-10-06T12:37:50.917 回答
1

对于 windows10 中的 gitbash,请注意其中的配置文件

/.gitconfig 文件

于 2019-09-26T13:45:34.133 回答
1

转到:C:\Program Files\Git\etc\profile.d 里面你找到所有你需要的

于 2021-12-28T22:42:27.897 回答
0

在 Windows 上:

你可以.bashrc在目录中找到:C:\Users\[YourUserName]

于 2021-09-17T20:13:57.453 回答
-5

有时文件实际上位于~/. 这些是我在Visual Studio Code /Windows 10上启动Zsh作为默认终端所采取的步骤。

  • cd ~/

  • vim .bashrc

  • 粘贴以下...

if test -t 1; then exec zsh fi

  • 保存/关闭 Vim。

  • 重启终端

于 2017-07-08T00:04:38.003 回答
-7

如果你想在打开 Git Bash 时有项目选择列表:

  • ppath在代码头中编辑您的 Git 项目路径,将此代码放入.bashrc文件中,然后将其复制到您的$HOME目录中(在 Windows Vista / Windows 7 中通常为C:\Users\$YOU

.

#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0

echo
echo -e "projects:\n-------------"

for f in *
do
    if [ -d "$f" ]
    then
        PROJECTS+=("$f")
        echo -e $((++i)) "- \e[1m$f\e[0m"
    fi
done


if [ ${#PROJECTS[@]} -gt 1 ]
then
    echo -ne "\nchoose project: "
    read proj
    case "$proj" in
        [0-`expr ${#PROJECTS[@]} - 1`]) cd "${PROJECTS[proj]}" ;;
        *) echo " wrong choice" ;;
    esac
else
    echo "there is no projects"
fi
unset PROJECTS
  • 您可能希望将此文件设置为Git Bash中的可执行文件,chmod +x .bashrc(但这可能是多余的,因为此文件存储在NTFS文件系统上)
于 2014-09-26T17:29:25.087 回答