如何为 git bash 创建别名以通过目录进行快速 cd

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27542006/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:01:57  来源:igfitidea点击:

How to create alias for git bash to make fast cd through directories

gitbashcmdgit-bash

提问by silver_rocket

I'm using git bash and let say i have 2 folders: C:/folder1 and C:/folder2, I'd like to make fast cd from one to another.

我正在使用 git bash,假设我有 2 个文件夹:C:/folder1 和 C:/folder2,我想从一个文件夹到另一个文件夹制作快速 cd。

I've tried git.alias but it seems to work for "git ..." commands only.. and I'm not very familiar with all this bash stuff.. so how should I do that?

我试过 git.alias 但它似乎只适用于“git ...”命令..而且我对所有这些 bash 的东西都不是很熟悉..那么我该怎么做呢?

回答by SMA

In bash you could do the following to create alias:

在 bash 中,您可以执行以下操作来创建别名:

alias cdf1="cd /user/home/cloudera"
cd /user
cdf1
pwd

You will get result of pwd as /user/home/cloudera. You could do the same in git-bash prompt as well with directory as "C:/folder1" instead of "/user/home/cloudera".

您将获得 pwd 的结果为 /user/home/cloudera。您也可以在 git-bash 提示符下使用“C:/folder1”而不是“/user/home/cloudera”作为目录执行相同的操作。

回答by MicroVirus

The following is to define aliases for Git Bash in Windows (MSYS Git)

下面是为Windows中的Git Bash定义别名(MSYS Git)

I finally managed to get aliasfor Git Bash working by placing a .bash_profilefile in my Windows user home directory (%USERPROFILE%, typically C:\Users\<username>\) and adding the following to it (example):

我终于alias通过.bash_profile在我的 Windows 用户主目录中放置一个文件(%USERPROFILE%通常为C:\Users\<username>\)并向其中添加以下内容(示例),设法让Git Bash 工作:

alias cdf1="d/Folder/Another\ folder\ with\ spaces/Destination/"

回答by Erikas Pliauksta

Windows users can create .bashrcfile in their users folder and set aliases there:

Windows 用户可以.bashrc在他们的用户文件夹中创建文件并在那里设置别名:

alias folder1='cd /c/folder1'

alias folder1='cd /c/folder1'

回答by VonC

You could simply define dos alias:

您可以简单地定义 dos 别名:

doskey cdf1=cd c:\folder1
doskey cdf2=cd c:\folder2

Then a simple cdf1would take you to the right folder, even in a git-cmdsession.
In a git-bash session, you would need bash alias, but you don't need a bash session to use git.

然后cdf1,即使在git-cmd会话中,一个简单的方法也会将您带到正确的文件夹。
git-bash session 中,您需要 bash 别名,但不需要 bash 会话即可使用 git。