如何在 Windows 中修改我的 Git Bash 配置文件?

Bri*_*ian 104 windows bashrc .bash-profile git-bash

我在 Windows 7 上使用 Git Bash,并希望设置 Bash 配置文件别名,alias gs='git status'以便让我的生活更轻松。我怎样才能做到这一点?

小智 132

当您打开 Git Bash 时,默认情况下您应该在您的主目录中。现在创建.bashrc文件(如果在 Windows 7 上,该文件应命名为.bashrc.)。

如果您不在主目录中,请键入以下内容更改为:

光盘

并按下Enter。cd,后面没有列出任何其他参数,将始终返回主目录。

您可以通过键入以下内容来创建文件:

触摸 .bashrc

然后用 Vim 编辑它,或者你可以尝试用一些 Windows 编辑器来做,但我不推荐它,因为一些文本格式问题。

vim .bashrc

i键更改为插入模式。

通过键入以下内容添加您的别名:

别名 gs='git status'

Esc键退出插入模式。

通过键入以下内容保存并关闭您的文件:wqEnter

:wEnter 只会保存您的文件。

:q!Enter 将退出编辑器而不保存您的文件。

最后,通过键入以下内容更新文件以使用您的新更改:

源.bashrc

  • 这给了我`警告:找到 ~/.bashrc 但没有 ~/.bash_profile、~/.bash_login 或 ~/.profile。这看起来像是不正确的设置。将为您创建一个加载 ~/.bashrc 的 ~/.bash_profile。` (Git-2.11.0-64-bit) (8认同)

gro*_*taj 34

您可以放入.bash_profile您的用户目录:C:\Users\<username>

您还可以创建一些只混帐的别名,所以你可以做git stgit status,加入这些行C:\ Users \ <用户名> \ gitconfig。

[alias]
st = status
Run Code Online (Sandbox Code Playgroud)

其他一些有用的别名:

cm = commit -m
cma = commit -a -m
br = branch
co = checkout
df = diff
ls = ls-files
sh = stash
sha = stash apply
shp = stash pop
shl = stash list
mg = merge
ph = push -u
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,“.bash_profile”*不在*我的 Windows 主目录中。但我的漫游主目录(网络管理员强加的)。 “正确”的答案是“将 `.bash_profile` 放入您的 *git bash* 主目录中”,您可以通过转到 `cd ~` 然后 `pwd` 找到它 (3认同)
  • 如果需要 Git 命令的别名,编辑 `.gitconfig` 文件通常就足够了。 (2认同)

小智 5

我的 git 版本是 git 版本 2.18.0.windows.1 我花了一段时间才弄清楚 .bashrc 在哪里 C:\Program Files\Git\etc ---> bash.bashrc 希望它有帮助