49

我正在运行 Windows 7 的笔记本电脑上使用最新的适用于 Windows 的 Git bash。当我定义我的别名时:

$ alias gitc='git commit -a'

会话期间一切正常,但如果我关闭并打开 bash,我将无法恢复它们。但是,命令历史记录被保留。

我该怎么办?我错过了什么?

谢谢!

4

8 回答 8

76

当您打开 git bash 输入命令时touch .bash_profile。继此型vim .bash_profile。然后,您可以将别名添加到此文件中。保存文件并重新打开 git bash,您的别名应该可以按预期工作。

此方法允许您为 git bash 中可用的任何 bash 命令创建别名,但是正如其他人回答的那样,也可以使用 git 本身创建特定于 git 的别名。

于 2012-12-05T02:04:02.217 回答
21

您可以设置一个 .gitconfig 并添加这样的别名,而不是修改您的 bash_profile:

[alias]
  st = status
  ci = commit
  br = branch
  co = checkout
  df = diff
  lg = log -p
于 2012-12-05T07:23:18.680 回答
11

在主目录中创建 .bashrc 文件:

touch ~/.bashrc
vim ~/.bashrc

在文件中~/.bashrc添加别名:

alias gitc='git commit -a'
# -- ... and your other aliases here ...

保存文件(<ESC>:wq在 vim 中按)。重新加载文件,以便 bash 知道所做的更改:

source ~/.bashrc

这些步骤在带有 Git bash (MINGW32) 的 Win 7/Win 8 中对我有用

于 2015-08-17T03:43:46.040 回答
5

对于Windows用户:
确保您在主目录中,最简单的方法是创建一个.bash_profile文件并在其中插入您的别名

注意:要使用记事本编辑它,请先执行此行:

git config core.editor notepad

然后创建文件并添加您的别名,如下所示:

notepad .bash_profile

现在您可以将别名添加到.bash_profilelike :

alias yourAlias='你的命令'
alias AnotherAlias='你的命令'


ctrl+s File>save 菜单保存文件

于 2015-11-04T23:13:34.427 回答
1

您需要将它们放在您的 .bash_profile 中。然后,每次新的登录 shell 启动时,它们都会被重置。

于 2012-12-05T02:03:50.513 回答
0

我知道你已经得到了答案,但你可能想考虑使用 git 自己的别名系统,这在 git config 帮助页面中有说明。然后它们可以是每个 repo 以及系统范围或每个用户。

于 2012-12-05T03:12:41.317 回答
0

我对powershell很陌生。我从公司借了一台笔记本电脑不到一周,这包括了我对 Windows 的全部体验。但是,我可能已经找到了一个可行的解决方案(至少对我来说)。

(灵感来自https://superuser.com/questions/1090141/does-powershell-have-any-sort-of-bashrc-equivalent

在 PowerShell 中:

New-Item $profile -Type File -Force

它会在 Documents 文件夹下名为 WindowsPowerShell 的文件夹中创建一个名为 Microsoft.PowerShell_profile.ps1 的文件。然后你可以用文本编辑器打开它:

notepad $profile

添加$bash = [bash | . ~/.bash_profile]到该文件。

现在,当从 powershell 输入 bash 时,您的 bash_profile 应该会自动获取。env例如,我可以键入bash_profile 中指定的所有局部变量。我想这也适用于.bashrc,但我还没有尝试过。

为了记录,这只是马文穆斯塔法答案的延伸。但是我不确定他的方法是否能解决我遇到的问题,即每次在 Windows 中启动新的命令提示符时,我都必须为我的 bash_profile 提供资源。

希望这是有用的。

**** 编辑 ****

该解决方案对我有用,但我因执行某些操作而收到来自 Windows 的投诉。我认为更好的解决方案是编辑/etc/bash.bashrc和添加source ~/.bash_profile. 完成此操作后,我的 PS1、配置文件变量等显示在env. 祝你好运。

于 2018-12-30T21:02:41.827 回答
0

如果你从这里有 git:https ://git-scm.com/download/win 转到已安装的文件夹。例如:“c:\Program Files\Git”

并找到aliases.sh通常位于此处的文件:C:\installation\folder\etc\profile.d\

现在您可以像这样为现有的别名添加别名:

# Some good standards, which are not used if the user
# creates his/her own .bashrc/.bash_profile

# --show-control-chars: help showing Korean or accented characters
alias ls='ls -F --color=auto --show-control-chars'
alias ll='ls -l'
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'

重新启动你的 bash 终端,现在你可以在每个 git 项目中使用你的新别名。

于 2021-11-05T19:08:27.683 回答