Windows 版 Git:.bashrc 或 Git Bash shell 的等效配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6883760/
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
Git for Windows: .bashrc or equivalent configuration files for Git Bash shell
提问by iftheshoefritz
I've just installed Git for Windows and am delighted to see that it installs Bash.
我刚刚为 Windows 安装了 Git,很高兴看到它安装了 Bash。
I want to customise the shell in the same way I can under Linux (e.g. set up aliases like ll
for ls -l
), but I can't seem to find .bashrc
or equivalent configuration files.
我想以与在 Linux 下相同的方式自定义 shell(例如设置像ll
for 的别名ls -l
),但我似乎找不到.bashrc
或等效的配置文件。
What should I be editing?
我应该编辑什么?
回答by Charles Ma
Create a .bashrc
file under ~/.bashrc
and away you go. Similarly for ~/.gitconfig
.
创建一个.bashrc
文件,~/.bashrc
然后离开。对于~/.gitconfig
.
~
is usually your C:\Users\<your user name>
folder. Typing echo ~
in the Git Bash terminal will tell you what that folder is.
~
通常是您的C:\Users\<your user name>
文件夹。键入echo ~
在Git的Bash的终端会告诉你该文件夹是什么。
If you can't create the file (e.g. running Windows), run the below command:
如果您无法创建文件(例如运行 Windows),请运行以下命令:
copy > ~/.bashrc
The window will output an error message (command not found), but the file will be created and ready for you to edit.
该窗口将输出一条错误消息(未找到命令),但该文件将被创建并可供您编辑。
回答by harsel
In newer versions of Git for Windows, Bash is started with --login
which causes Bash to not read .bashrc
directly. Instead it reads .bash_profile
.
在适用于 Windows 的较新版本的 Git 中,Bash 开始时--login
会导致 Bash 无法.bashrc
直接读取。相反,它读取.bash_profile
。
If this file does not exist, create it with the following content:
如果此文件不存在,请使用以下内容创建它:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
This will cause Bash to read the .bashrc
file. From my understanding of this issue, Git for Windows should do this automatically. However, I just installed version 2.5.1, and it did not.
这将导致 Bash 读取.bashrc
文件。根据我对这个问题的理解,Git for Windows 应该自动执行此操作。但是,我刚刚安装了 2.5.1 版,但没有安装。
回答by Stan S.
I had to add a user environment variable, HOME
, with C:\Users\<your user name>
by going to System, Advanced System Settings, in the System Propertieswindow, the Advancedtab, Environment Variables...
我不得不添加一个用户环境变量HOME
,以C:\Users\<your user name>
通过将系统,高级系统设置,在系统属性窗口,在高级选项卡,环境变量...
Then in my C:\Users\<your user name>
I created the file .bashrc
, e.g., touch .bashrc
and added the desired aliases.
然后在我C:\Users\<your user name>
的文件中.bashrc
,例如,touch .bashrc
并添加了所需的别名。
回答by ThePatelGuy
I think the question here is how to find .bashrc file on Windows.
我认为这里的问题是如何在 Windows 上找到 .bashrc 文件。
Since you are using Windows, you can simply use commands like
由于您使用的是 Windows,您可以简单地使用如下命令
start .
OR
或者
explorer .
to open the window with the root directory of your Git Bash installation where you'll find the .bashrc
file. You may need to create one if it doesn't exist.
打开包含 Git Bash 安装根目录的窗口,您将在其中找到该.bashrc
文件。如果它不存在,您可能需要创建一个。
You can use Windows tools like Notepad++ to edit the file instead of using Vim in your Bash window.
您可以使用 Notepad++ 等 Windows 工具来编辑文件,而不是在 Bash 窗口中使用 Vim。
回答by Ruben Arevalo
1) Start by opening up git-bash.exe in Administrator mode. (Right click the file and select "Run as Administrator", or change settings in Properties → Compatibility → Run this program as administrator.)
1) 首先在管理员模式下打开 git-bash.exe。(右键单击该文件并选择“以管理员身份运行”,或在“属性”→“兼容性”→“以管理员身份运行此程序”中更改设置。)
2) Run cd ~
. It will take you to C:/Users/<Your-Username>
.
2)运行cd ~
。它会带你去C:/Users/<Your-Username>
。
3) Run vi .bashrc
. This will open you up into the editor. Hit INSERT and then start entering the following info:
3)运行vi .bashrc
。这将打开您进入编辑器。点击 INSERT,然后开始输入以下信息:
alias ll="ls -la" # this changes the default ll on git bash to see hidden files.
cd "C:\directory\to\your\work\path\"
ll # this shows your your directory before you even type anything.
回答by Ayan
Just notepad ~/.bashrc
from the git bash shell and save your file.That should be all.
只是notepad ~/.bashrc
从 git bash shell 并保存您的文件。这应该是全部。
NOTE: Please ensure that you need to restart your terminal for changes to be reflected.
注意:请确保您需要重新启动终端才能反映更改。
回答by Arun K
for gitbash in windows10 look out for the config file in
对于 windows10 中的 gitbash,请注意中的配置文件
/.gitconfig file
/.gitconfig 文件
回答by Sheldon Oliveira
Don't need to create a new file, it is already there!
不需要创建新文件,它已经在那里了!
/etc/bash.bashrc
回答by Brandon
Sometimes the files are actually located at ~/
. These are the steps I took to starting Zshas the default terminal on Visual Studio Code/Windows 10.
有时文件实际上位于~/
. 这些是我在Visual Studio Code/Windows 10上启动Zsh作为默认终端所采取的步骤。
cd ~/
vim .bashrc
Paste the following...
cd ~/
vim .bashrc
粘贴以下...
if test -t 1; then
exec zsh
fi
if test -t 1; then
exec zsh
fi
Save/close Vim.
Restart the terminal
保存/关闭 Vim。
重启终端
回答by s1w_
If you want to have projects choice listwhen you open Git Bash:
如果你想在打开 Git Bash 时有项目选择列表:
- Edit
ppath
in the code header to your Git projects path, put this code into .bashrcfile, and copy it into your $HOMEdirectory (in Windows Vista / Windows 7 it is often C:\Users\$YOU)
ppath
在代码头中编辑您的 Git 项目路径,将此代码放入.bashrc文件中,并将其复制到您的$HOME目录中(在 Windows Vista / Windows 7 中,它通常是C:\Users\$YOU)
.
.
#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0
echo
echo -e "projects:\n-------------"
for f in *
do
if [ -d "$f" ]
then
PROJECTS+=("$f")
echo -e $((++i)) "- \e[1m$f\e[0m"
fi
done
if [ ${#PROJECTS[@]} -gt 1 ]
then
echo -ne "\nchoose project: "
read proj
case "$proj" in
[0-`expr ${#PROJECTS[@]} - 1`]) cd "${PROJECTS[proj]}" ;;
*) echo " wrong choice" ;;
esac
else
echo "there is no projects"
fi
unset PROJECTS
- You may want set this file as executable inside Git Bash, chmod +x .bashrc(but it's probably redundant, since this file is stored on an NTFSfilesystem)
- 您可能希望将此文件设置为Git Bash 中的可执行文件,chmod +x .bashrc(但它可能是多余的,因为此文件存储在NTFS文件系统上)