如何在 Windows 版 Git Bash 中设置别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37104273/
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
How to set aliases in the Git Bash for Windows?
提问by Sruj_2ndAccountForStupidQtions
How to alias command in Git Bash for Windows downloaded from git-scm.com ?
如何在从 git-scm.com 下载的 Windows 版 Git Bash 中别名命令?
I mean Bash commands not Git.
我的意思是 Bash 命令不是 Git。
(windows7)
(Windows 7的)
Edit:
编辑:
Writing aliases in .bashrc
file (as suggested by @gturri) not adding it in console.(after system reboot)(I have never wrote alias for ls
command so it should be some default alias.)
在.bashrc
文件中写入别名(如@gturri所建议的)而不是在控制台中添加它。(系统重启后)(我从来没有为ls
命令写过别名,所以它应该是一些默认别名。)
回答by gturri
To configure bash aliases, it's the same as if you were on a Unix platform: put them in a .bashrc
in your home:
要配置 bash 别名,就像您在 Unix 平台上一样:将它们.bashrc
放在您家中的 a中:
cd
echo alias ll=\'ls -l\' >> .bashrc
To have this change taken into account you should then either source this file (ie: run source .bashrc
) or restart your terminal
要考虑此更改,您应该获取此文件(即: run source .bashrc
)或重新启动终端
(In some cases* you can find equivalent for .bashrc
file in C:\Users\<username>\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh.
And you should add aliases in aliases.sh.
)
(在某些情况下*您可以在中找到.bashrc
文件的等效项C:\Users\<username>\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh.
并且您应该在中添加别名aliases.sh.
)
(*this case is when you install Git for Windows GUI release from https://git-scm.com/download/winthat contains GitBash)
(*这种情况是当您从包含 GitBash 的https://git-scm.com/download/win安装适用于 Windows GUI 版本的 Git 时)
回答by Maurizio
I had the same problem, I can't figured out how to find the aliases used by Git Bash on Windows.
After searching for a while, I found the aliases.shfile under C:\Program Files\Git\etc\profile.d\aliases.sh
.
我遇到了同样的问题,我不知道如何在 Windows 上找到 Git Bash 使用的别名。搜索了一会,找到了.sh下的aliases.sh文件C:\Program Files\Git\etc\profile.d\aliases.sh
。
This is the path under windows 7, maybe can be different in other installation.
这是windows 7下的路径,其他安装可能会有所不同。
Just open it with your preferred editor in admin mode. After save it, reload your command prompt.
只需在管理模式下使用您喜欢的编辑器打开它。保存后,重新加载命令提示符。
I hope this can help!
我希望这能有所帮助!
回答by maheshwaghmare
Follow below steps:
请按照以下步骤操作:
Open the file
.bashrc
which is found in locationC:\Users\USERNAME\.bashrc
If file
.bashrc
not exist then create it using below steps:- Open Command Prompt and goto
C:\Users\USERNAME\
. - Type command
notepad ~/.bashrc
It generates the.bashrc
file.
- Open Command Prompt and goto
Add below sample commands of WP CLI, Git, Grunt & PHPCS etc.
打开
.bashrc
在位置找到的文件C:\Users\USERNAME\.bashrc
如果文件
.bashrc
不存在,则使用以下步骤创建它:- 打开命令提示符并转到
C:\Users\USERNAME\
. - 键入命令
notepad ~/.bashrc
它生成.bashrc
文件。
- 打开命令提示符并转到
添加以下 WP CLI、Git、Grunt 和 PHPCS 等示例命令。
# ----------------------
# Git Command Aliases
# ----------------------
alias ga='git add'
alias gaa='git add .'
alias gaaa='git add --all'
# ----------------------
# WP CLI
# ----------------------
alias wpthl='wp theme list'
alias wppll='wp plugin list'
Now you can use the commands:
现在您可以使用以下命令:
ga
instead ofgit add .
wpthl
instead ofwp theme list
ga
代替git add .
wpthl
代替wp theme list
Eg. I have used wpthl
for the WP CLI command wp theme list
.
例如。我已经用于wpthl
WP CLI 命令wp theme list
。
Yum@M MINGW64 /c/xampp/htdocs/dev.test
$ wpthl
+------------------------+----------+-----------+----------+
| name | status | update | version |
+------------------------+----------+-----------+----------+
| twentyeleven | inactive | none | 2.8 |
| twentyfifteen | inactive | none | 2.0 |
| twentyfourteen | inactive | none | 2.2 |
| twentyseventeen | inactive | available | 1.6 |
| twentysixteen | inactive | none | 1.5 |
| twentyten | inactive | none | 2.5 |
| twentythirteen | inactive | none | 2.4 |
| twentytwelve | inactive | none | 2.5 |
For more details read the article Keyboard shortcut/aliases for the WP CLI, Git, Grunt & PHPCS commands for windows
有关更多详细信息,请阅读适用于 Windows 的 WP CLI、Git、Grunt 和 PHPCS 命令的键盘快捷键/别名一文
回答by CodeWizard
回答by npcoder
There is two easy way to set the alias.
有两种简单的方法可以设置别名。
- Using Bash
- Updating .gitconfig file
- 使用 Bash
- 更新 .gitconfig 文件
Using Bash
使用 Bash
Open bash terminal and type git command. For instance:
打开 bash 终端并输入 git 命令。例如:
$ git config --global alias.a add
$ git config --global alias.aa 'add .'
$ git config --global alias.cm 'commit -m'
$ git config --global alias.s status
---
---
It will eventually add those aliases on .gitconfig file.
它最终会在 .gitconfig 文件中添加这些别名。
Updating .gitconfig file
更新 .gitconfig 文件
Open .gitconfig file located at 'C:\Users\username\.gitconfig' in Windows environment. Then add following lines:
在 Windows 环境中打开位于 'C:\Users\username\.gitconfig' 的 .gitconfig 文件。然后添加以下几行:
[alias]
a = add
aa = add .
cm = commit -m
gau = add --update
au = add --update
b = branch
---
---
回答by user3393813
Go to:
C:\Users\ [youruserdirectory] \bash_profile
In your bash_profile file type - alias desk='cd " [DIRECTORY LOCATION] "'
Refresh your User directory where the bash_profile file exists then reopen your CMD or Git Bash window
去:
C:\Users\ [youruserdirectory] \bash_profile
在你的 bash_profile 文件类型中 - alias desk='cd " [DIRECTORY LOCATION] "'
刷新 bash_profile 文件所在的用户目录,然后重新打开 CMD 或 Git Bash 窗口
Type in desk to see if you get to the Desktop location or the location you want in the "DIRECTORY LOCATION" area above
在上面的“DIRECTORY LOCATION”区域中输入desk以查看您是否到达了桌面位置或您想要的位置
Note: [ desk ] can be what ever name that you choose and should get you to the location you want to get to when typed in the CMD window.
注意:[ 桌面 ] 可以是您选择的任何名称,并且在 CMD 窗口中键入时应该可以让您到达您想要到达的位置。
回答by shubhamr238
To Add a Temporary Alias:
添加临时别名:
- Goto Terminal (I'm using git bash for windows).
- Type
$ alias gpuom='git push origin master'
- To See a List of All the aliases type
$ alias
hit Enter.
- 转到终端(我在 Windows 上使用 git bash)。
- 类型
$ alias gpuom='git push origin master'
- 要查看所有别名的列表,请按
$ alias
Enter 键。
To Add a Permanent Alias:
添加永久别名:
- Goto Terminal (I'm using git bash for windows).
- Type
$ vim ~/.bashrc
and hit Enter (I'm guessing you are familiar with vim). - Add your new aliases (For reference look at the snippet below).
#My custom aliases alias gpuom='git push origin master' alias gplom='git pull origin master'
- Save and Exit (Press Esc then type :wq).
- To See a List of All the aliases type
$ alias
hit Enter.
- 转到终端(我在 Windows 上使用 git bash)。
- 键入
$ vim ~/.bashrc
并按 Enter(我猜您对 vim 很熟悉)。 - 添加您的新别名(有关参考,请查看下面的代码段)。
#My custom aliases alias gpuom='git push origin master' alias gplom='git pull origin master'
- 保存并退出(按 Esc 然后键入 :wq)。
- 要查看所有别名的列表,请按
$ alias
Enter 键。