简体   繁体   English

如何在 Git Bash 中为 Windows 设置别名?

[英]How to set aliases in the Git Bash for Windows?

How to alias command in Git Bash for Windows downloaded from git-scm.com?如何在 Git Bash 中为从 git-scm 下载的 Windows 中的命令设置别名。

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命令写过别名,所以它应该是一些默认别名。)

.bashrc。

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中:

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要考虑到此更改,您应该获取此文件(即:运行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. ) (在某些情况下*您可以在C:\Users\<username>\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh.中找到.bashrc文件的等效项。您应该在aliases.sh.中添加别名aliases.sh. )

(*this case is when you install Git for Windows GUI release from https://git-scm.com/download/win that contains GitBash) (*这种情况是当您从包含 GitBash 的https://git-scm.com/download/win安装适用于 Windows GUI 版本的 Git 时)

I had the same problem, I can't figured out how to find the aliases used by Git Bash on Windows.我遇到了同样的问题,我不知道如何在 Windows 上找到 Git Bash 使用的别名。 After searching for a while, I found the aliases.sh file under C:\Program Files\Git\etc\profile.d\aliases.sh .找了一会,找到了C:\Program Files\Git\etc\profile.d\aliases.sh下的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!我希望这能有所帮助!

Follow below steps:请按照以下步骤操作:

  1. Open the file .bashrc which is found in location C:\Users\USERNAME\.bashrc打开位于C:\Users\USERNAME\.bashrc中的文件.bashrc

    If file .bashrc not exist then create it using below steps:如果文件.bashrc不存在,则使用以下步骤创建它:

    1. Open Command Prompt and goto C:\Users\USERNAME\ .打开命令提示符并转到C:\Users\USERNAME\
    2. Type command notepad ~/.bashrc键入命令notepad ~/.bashrc
      It generates the .bashrc file.它生成.bashrc文件。
  2. Add below sample commands of WP CLI, Git, Grunt & PHPCS etc.添加下面的 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 of git add . ga而不是git add .
  • wpthl instead of wp theme list 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有关更多详细信息,请阅读文章WP CLI、Git、Grunt 和 PHPCS 命令的 Windows 键盘快捷键/别名

You can add it manually in the .gitconfig file您可以在 .gitconfig 文件中手动添加它

[alias]
    cm = "commit -m"

Or using the script:或使用脚本:

git config --global alias.cm "commit -m"

Here is a screenshot of the .gitconfig这是.gitconfig的屏幕截图

在此处输入图像描述

To Add a Temporary Alias:添加临时别名:

  1. Goto Terminal (I'm using git bash for windows).转到终端(我在 Windows 上使用 git bash)。
  2. Type $ alias gpuom='git push origin master' $ alias gpuom='git push origin master'
  3. To See a List of All the aliases type $ alias hit Enter.要查看所有别名的列表,请键入$ alias回车。

To Add a Permanent Alias:要添加永久别名:

  1. Goto Terminal (I'm using git bash for windows).转到终端(我在 Windows 上使用 git bash)。
  2. Type $ vim ~/.bashrc and hit Enter (I'm guessing you are familiar with vim).输入$ vim ~/.bashrc并按 Enter(我猜你对 vim 很熟悉)。
  3. 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'
  4. Save and Exit (Press Esc then type :wq).保存并退出(按 Esc 然后输入 :wq)。
  5. To See a List of All the aliases type $ alias hit Enter.要查看所有别名的列表,请键入$ alias回车。

There is two easy way to set the alias.有两种简单的方法来设置别名。

  1. Using Bash使用 Bash
  2. Updating .gitconfig file更新 .gitconfig 文件

Using Bash使用 Bash

Open bash terminal and type git command.打开 bash 终端并输入 git 命令。 For instance:例如:

$ 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.在 Windows 环境中打开位于 'C:\Users\username\.gitconfig' 的 .gitconfig 文件。 Then add following lines:然后添加以下行:

[alias]  
a = add  
aa = add . 
cm = commit -m 
gau = add --update 
au = add --update
b = branch
---
---
  1. Go to C:\Program Files\Git\etc\profile.d转到C:\Program Files\Git\etc\profile.d

  2. Open as aliases.sh with your text editor as Administrator .使用您的文本编辑器以Administrator身份打开aliases.sh

  3. Add your new aliases and save添加新别名并保存

  4. Restart git bash重启 git bash

Source: https://dev.to/mhjaafar/git-bash-on-windows-adding-a-permanent-alias-198g来源: https ://dev.to/mhjaafar/git-bash-on-windows-adding-a-permanent-alias-198g

  • Go to: C:\Users\ [youruserdirectory] \bash_profile转到: C:\Users\ [youruserdirectory] \bash_profile

  • In your bash_profile file type - alias desk='cd " [DIRECTORY LOCATION] "'在您的 bash_profile 文件类型中 - alias desk='cd " [DIRECTORY LOCATION] "'

  • Refresh your User directory where the bash_profile file exists then reopen your CMD or Git Bash window刷新 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”区域中您想要的位置

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.注意:[desk] 可以是您选择的任何名称,当您在 CMD 窗口中键入时,应该可以将您带到您想要到达的位置。

Using Windows and MINGW64 GitBash (mintty 3.2.0), I found the file under:使用 Windows 和 MINGW64 GitBash (mintty 3.2.0),我在下面找到了文件:

%LocalAppData%\Programs\Git\etc\profile.d\aliases.sh

Just added the alias there and it worked for me.刚刚在那里添加了别名,它对我有用。

I would prefer a zero-setup solution, but i found none yet.我更喜欢零设置解决方案,但我还没有找到。 Once i Pull a repository, how can automagically have some standard alias available, so Bash shell for Git Extension can recognize it ?一旦我拉出一个存储库,如何自动获得一些可用的标准别名,以便 Git 扩展的 Bash shell 可以识别它? It seem that the C:\User<youruser>.bashrd is recognized, and also C:\Program Files\Git\etc\profile.d\aliases.sh, but these locations are outside the pulled project, and need to be manually configured.好像 C:\User<youruser>.bashrd 被识别了,还有 C:\Program Files\Git\etc\profile.d\aliases.sh,但是这些位置在拉取的项目之外,需要手动配置。

Do exist some way to "autoconfigure" aliases for bash shell (for windows git extensions) just dropping a file in the project root, and not somewhere into user settings ?是否存在某种方式来“自动配置”bash shell(用于 windows git 扩展)的别名,只是将文件放在项目根目录中,而不是在用户设置的某个地方?

For people who don't have admin privileges to make the changes mentioned in other solutions can do this easy trick.对于没有管理员权限来进行其他解决方案中提到的更改的人来说,可以做到这一点。

  1. Create a file .bash_profile with all the aliases under the folder C:\Users\<user-profile>在文件夹C:\Users\<user-profile>下创建一个包含所有别名的文件.bash_profile
  2. Now restart your git bash and your shortcuts should work.现在重新启动您的 git bash,您的快捷方式应该可以使用。

Note: I am using windows 10注意:我使用的是 windows 10

These commands work for WSL for Ubuntu:这些命令适用于 Ubuntu 的 WSL:

  1. Open .bashrc file: vim ~/.bashrc打开.bashrc文件: vim ~/.bashrc
  2. Add the following commands:添加以下命令:
    alias gt="git status"
    alias ll='"ls -la"
    alias gp="git pull"
  3. Save and exit the file: :wq保存并退出文件:wq
  4. And then source the file: source ~/.bashrc然后源文件: source ~/.bashrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM