我的 Git bash 忘记了我的别名。我能做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13715179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My Git bash forgets my aliases. What can I do?
提问by Antoine Lizée
I am working with the latest Git bash for Windows, on my laptop running Windows 7. When I define my aliases like:
我正在运行 Windows 7 的笔记本电脑上使用适用于 Windows 的最新 Git bash。当我定义别名时,例如:
$ alias gitc='git commit -a'
Everything works well during the session, but I cannot recover them if I close and open the bash. The command history is preserved, though.
在会话期间一切正常,但如果我关闭并打开 bash,我将无法恢复它们。但是,命令历史记录被保留。
What should I do? What I have missed?
我该怎么办?我错过了什么?
Thanks!
谢谢!
回答by ctor
When you open the git bash type in the command touch .bash_profile
.
Following this type vim .bash_profile
.
You can then add your aliases to this file. Save the file and reopen the git bash and your aliases should work as expected.
当您在命令中打开 git bash 类型时touch .bash_profile
。以下这种类型vim .bash_profile
。然后,您可以将别名添加到此文件中。保存文件并重新打开 git bash,您的别名应该可以正常工作。
This method allows you to create aliases for any bash command available in git bash however as others have answered it is also possible to create git specific aliases using git itself.
此方法允许您为 git bash 中可用的任何 bash 命令创建别名,但是正如其他人所回答的那样,也可以使用 git 本身创建特定于 git 的别名。
回答by AJ.
Instead of modifying your bash_profile you can setup a .gitconfig and add aliases like this:
您可以设置一个 .gitconfig 并添加这样的别名,而不是修改您的 bash_profile:
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
回答by Sam Su
Create the .bashrc file in the home directory:
在主目录中创建 .bashrc 文件:
touch ~/.bashrc
vim ~/.bashrc
In the file ~/.bashrc
add the aliases:
在文件中~/.bashrc
添加别名:
alias gitc='git commit -a'
# -- ... and your other aliases here ...
Save the file (press <ESC>:wq
in vim). Reload the file such that bash is aware of the changes made:
保存文件(<ESC>:wq
在 vim 中按下)。重新加载文件,以便 bash 知道所做的更改:
source ~/.bashrc
These steps work for me in Win 7/Win 8 with Git bash (MINGW32)
这些步骤在使用 Git bash (MINGW32) 的 Win 7/Win 8 中对我有用
回答by Marvin Mustafa
For WindowsUsers :
Be sure you're in the home directory,
the Simplest wayis creating a .bash_profile
File and insert your aliases within
对于Windows用户:
确保您在主目录中,最简单的方法是创建一个.bash_profile
文件并在其中插入您的别名
note : to edit it with Notepad execute this line first :
注意:要使用记事本编辑它,请先执行此行:
git config core.editor notepad
git config core.editor notepad
then create the file and add your alias as following :
然后创建文件并添加您的别名,如下所示:
notepad .bash_profile
notepad .bash_profile
now you can add your aliases to the .bash_profile
like :
现在您可以将您的别名添加到.bash_profile
like 中:
alias yourAlias='your Command Here'
alias AnotherAlias='your Command Here'
alias yourAlias='your Command Here'
alias AnotherAlias='your Command Here'
save the file by pressing ctrl+sorFile>save
Menu
按ctrl+s或File>save
Menu保存文件
回答by khagler
You need to put them in your .bash_profile. Then they'll get reset every time a new login shell starts up.
您需要将它们放在您的 .bash_profile 中。然后,每次新的登录 shell 启动时,它们都会被重置。
回答by siride
I know you've already gotten an answer, but you might want to consider using git's own alias system, which is explained in the git config help page. Then they can be per-repo as well as system-wide or per-user.
我知道您已经得到了答案,但您可能需要考虑使用 git 自己的别名系统,这在 git config 帮助页面中进行了解释。然后它们可以是每个存储库以及系统范围或每个用户。
回答by Christian Meyer
I'm very new to powershell. I've been borrowing a laptop from my company for less than a week, and that encompasses my entire experience with windows. However, I may have found a solution that works (at least for me).
我对 powershell 很陌生。我从公司借了一台笔记本电脑还不到一周,这涵盖了我使用 Windows 的全部经验。但是,我可能已经找到了一个有效的解决方案(至少对我而言)。
(inspired by https://superuser.com/questions/1090141/does-powershell-have-any-sort-of-bashrc-equivalent)
(灵感来自https://superuser.com/questions/1090141/does-powershell-have-any-sort-of-bashrc-equivalent)
In PowerShell:
在 PowerShell 中:
New-Item $profile -Type File -Force
New-Item $profile -Type File -Force
It creates a file called Microsoft.PowerShell_profile.ps1 in a folder called WindowsPowerShell under your Documents folder. Then you can open it with a text editor:
它会在 Documents 文件夹下名为 WindowsPowerShell 的文件夹中创建一个名为 Microsoft.PowerShell_profile.ps1 的文件。然后你可以用文本编辑器打开它:
notepad $profile
notepad $profile
Add $bash = [bash | . ~/.bash_profile]
to that file.
添加$bash = [bash | . ~/.bash_profile]
到该文件。
Now when entering bash from powershell, your bash_profile should be sourced automatically. I can type env
, for example, and all of the local variables specified in my bash_profile are present. I imagine that this would also work with .bashrc, but I haven't tried that yet.
现在,当从 powershell 输入 bash 时,您的 bash_profile 应该会自动获取。env
例如,我可以键入,并且我的 bash_profile 中指定的所有局部变量都存在。我想这也适用于 .bashrc,但我还没有尝试过。
For the record, this is just an extension of Marvin Mustafa's answer. But I'm not sure that his method would fix the problem I was having, which was that I had to resource my bash_profile every time I started a new command prompt in windows.
根据记录,这只是 Marvin Mustafa 答案的延伸。但我不确定他的方法是否能解决我遇到的问题,即每次我在 Windows 中启动新的命令提示符时,我都必须为我的 bash_profile 提供资源。
Hope this is useful.
希望这是有用的。
**** EDIT ****
**** 编辑 ****
This solution worked for me but I got complaints from windows from doing certain actions. I think the better solution, although potentially problematic as well, is to edit /etc/bash.bashrc
and add source ~/.bash_profile
. After doing this my PS1, profile variables, etc. were shown in env
. Good Luck.
此解决方案对我有用,但我因执行某些操作而收到来自 Windows 的投诉。我认为更好的解决方案是编辑/etc/bash.bashrc
和添加source ~/.bash_profile
. 执行此操作后,我的 PS1、配置文件变量等显示在env
. 祝你好运。