如何避免在每个 Git 命令的开头输入“git”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56505000/
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 do I avoid typing "git" at the begining of every Git command?
提问by Nocturnal
I'm wondering if there's a way to avoid having to type the word git
at the beginning of every Git command.
我想知道是否有办法避免git
在每个 Git 命令的开头输入单词。
It would be nice if there was a way to use the git
command only once in the beginning after opening a command prompt to get into "Git mode".
如果有一种方法可以git
在打开命令提示符进入“Git 模式”后仅在开始时使用该命令一次,那就太好了。
For example:
例如:
git>
After which every command we type is by default interpreted as a Git command.
之后我们输入的每个命令默认都被解释为 Git 命令。
In a way similar to how we use the MySQL shell to write database commands:
类似于我们使用 MySQL shell 编写数据库命令的方式:
mysql>
This will save me from having to type git
hundreds of times a day.
这将使我不必git
每天打字数百次。
NOTE:I'm using git-bash
, on Windows.
注意:我git-bash
在 Windows 上使用, 。
回答by alfunx
You might want to try gitsh. From their readme:
您可能想尝试gitsh。从他们的自述文件:
The
gitsh
program is an interactive shell for git. From withingitsh
you can issue any git command, even using your local aliases and configuration.
- Git commands tend to come in groups. Avoid typing
git
over and over and over by running them in a dedicated git shell:sh$ gitsh gitsh% status gitsh% add . gitsh% commit -m "Ship it!" gitsh% push gitsh% ctrl-d sh$
该
gitsh
程序是 git 的交互式 shell。gitsh
您可以从内部发出任何 git 命令,甚至可以使用您的本地别名和配置。
- Git 命令往往成组出现。
git
通过在专用的 git shell 中运行它们来避免一遍又一遍地输入:sh$ gitsh gitsh% status gitsh% add . gitsh% commit -m "Ship it!" gitsh% push gitsh% ctrl-d sh$
Or have a look at the other projects linked there:
或者看看那里链接的其他项目:
Note: Haven't used this myself.
注意:我自己没用过。
回答by Nocturnal
A Perl one-liner which will do this:
Perl one-liner 将执行此操作:
perl -nE 'BEGIN {print "git > "} system "git $_"; print "git > "'
This will execute whatever you type, prefixed with git
. And it will keep doing that until you hit ^D
.
这将执行您键入的任何内容,前缀为git
. 它会一直这样做,直到您点击^D
。
回答by Thomas
This is not exactly what you're asking for, but you could set up some shell aliases in your ~/.bashrc
for the Git commands you use most frequently:
这并不完全是您所要求的,但是您可以在您~/.bashrc
最常使用的 Git 命令中设置一些 shell 别名:
alias commit='git commit'
alias checkout='git checkout'
...
Also note that you can create aliases within Git itself:
另请注意,您可以在 Git 本身内创建别名:
git config --global alias.ci commit
git config --global alias.co checkout
...
This lets you type git ci
instead of git commit
, and so on.
这使您可以键入git ci
而不是git commit
,等等。
回答by JacobIRR
I'm a big fan of using aliases in ~/.bash_profile for my GitBash. If you go with this approach, here are some of my favorites:
我非常喜欢在 ~/.bash_profile 中为我的 GitBash 使用别名。如果你采用这种方法,这里有一些我最喜欢的:
# git
alias gw='git whatchanged'
alias gg='git grep -n -C8'
alias ggi='git grep -i -n -C8'
alias gb='git branch'
alias gbd='git branch -D'
alias gba='git branch -a'
alias gc='git checkout'
alias gcp='git cherry-pick'
alias gfo='git fetch origin'
alias s='git status'
alias gmom='git merge origin/master'
alias grom='git rebase origin/master'
alias gpom='git pull origin master'
alias pplog='git log --oneline --graph --decorate'
回答by prosti
回答by Sam Weaver
A friend of mine made a small bash script that accomplishes this. It's called Replify.
我的一个朋友制作了一个小 bash 脚本来完成这个任务。它被称为Replify。
$ replify git
Initialized REPL for [git]
git> init
Initialized empty Git repository in /your/directory/here/.git/
git> remote add origin https://your-url/repo.git
git> checkout -b new-branch
Switched to a new branch 'new-branch'
git> push
回答by nomadictype
Here is another way. It's also not quite what was asked, but I've been using it for some time and it is pretty nice. Add the following line to your ~/.bashrc
:
这是另一种方式。这也不完全是被问到的,但我已经使用了一段时间,它非常好。将以下行添加到您的~/.bashrc
:
complete -E -W git
Now pressing Tab at an empty Bash prompt will type out "git ".
现在在空的 Bash 提示符下按 Tab 将输入“git”。
回答by LogicalBranch
I know this is a very late answer but this question really struck a note with me because I've been dealing withsuffering from this kind of repetition for quite a while now.
我知道这是一个很晚的答案,但这个问题确实给我留下了深刻的印象,因为我已经遭受这种重复的痛苦已经有一段时间了。
I'm not sure about you but I honestly don't(I repeat DON'T) want to create aliases for every git
command, so instead I wrote a python script called NoGitto solve this problem:
我不确定你,但老实说,我不想(我再说一遍DON'T)不想为每个git
命令创建别名,所以我写了一个名为NoGit的 python 脚本来解决这个问题:
#!/usr/bin/env python
import sys, os, signal, atexit, readline, subprocess
commands, stop, history_file = [], False, os.path.join(os.getcwd(), "git.history")
def run_commands():
stop = True
for cmd in commands:
command = ["git" if not cmd.startswith("git ") else ""]
command = [cmd] if command[0] == "" else [command[0], cmd]
subprocess.Popen(command).communicate()
commands = []
def signal_handler(sig, frame):
run_commands()
sys.exit(0)
try:
readline.read_history_file(history_file)
signal.signal(signal.SIGINT, signal_handler)
while True:
if stop == True:
break
command = input("git> ")
if command == "%undo":
commands.pop()
elif command == "%run":
run_commands()
elif command == "%exit":
sys.exit(0)
else:
commands += [cmd.strip() for cmd in command.split(";")]
signal.pause()
readline.set_history_length(-1)
except IOError:
pass
atexit.register(readline.write_history_file, history_file)
NoGitis a simple python script to prevent the unnecessary repetition of the "git" keyword.
NoGit是一个简单的 python 脚本,用于防止“git”关键字的不必要重复。
Documentation:
文档:
- the
%undo
command removes the last command from the stack - the
%run
command runs the commands in the stack and clears the stack - the
%exit
command closes the CLI without doing anything - pressing
ctr+c
is the same as running%run; %exit
- the script saves commands that were executed to a file called
git.history
in the same folder as the script - you can add multiple commands in one line using a semi-colon
- you can use the keyword
git
in the beginning of the command and the script won't duplicate it (E.G:git init
doesn't becomegit git init
)
- 该
%undo
命令从堆栈中删除最后一个命令 - 该
%run
命令运行在堆栈中的命令和清除堆 - 该
%exit
命令不做任何事情就关闭 CLI - 按下
ctr+c
和跑步一样%run; %exit
- 脚本将执行的命令保存到
git.history
与脚本位于同一文件夹中的文件中 - 您可以使用分号在一行中添加多个命令
- 您可以
git
在命令的开头使用关键字,脚本不会复制它(例如:git init
不会变成git git init
)
Example commands:
示例命令:
init
add .
stage .
commit -m "inital commit"
%run; %exit
init
add .
stage .
commit -m "inital commit"
%run; %exit
Additional information (for Linux users):
附加信息(适用于 Linux 用户):
If you want you can remove the .py
extension and convert it into an executable using:
如果需要,您可以删除.py
扩展并将其转换为可执行文件:
mv ./git.py ./git
chmod +x ./git
Then instead of calling the script like this:
然后,而不是像这样调用脚本:
python3 git.py
You'd run this instead:
你会运行这个:
./git
Additional information (for lazy people):
附加信息(懒人):
If you're lazy and don't want to type out a ./
then you could move this script to your /bin/
folder and create an alias for it.
如果您很懒惰并且不想输入 a./
那么您可以将此脚本移动到您的/bin/
文件夹并为其创建别名。
If you're really, reallylazy, use the following commands:
如果你真的非常懒惰,请使用以下命令:
sudo cp ./git /bin/nogit
sudo chmod +x /bin/nogit
alias nogit='/bin/nogit'
If you're really, really, reallylazy, copy and paste the following one-liner:
如果您真的非常非常懒惰,请复制并粘贴以下一行代码:
sudo cp ./git /bin/nogit && sudo chmod +x /bin/nogit && alias nogit='/bin/nogit'
If your laziness has reached levels previously unknown to humanity, here is a more compact version of the same one-liner:
如果你的懒惰达到了人类以前未知的程度,这里有一个更紧凑的版本:
sudo cp ./git /bin/nogit;sudo chmod +x /bin/nogit;alias nogit='/bin/nogit'
Good luck.
祝你好运。
回答by Lie Ryan
Another approach that will work with any commands: use Ctrl+R (reverse-i-search).
另一种适用于任何命令的方法:使用 Ctrl+R(反向搜索)。
The reverse-i-search allows you to search your command history. Repeat Ctrl+R after pressing your search string to repeat search further back with the same string.
reverse-i-search 允许您搜索您的命令历史记录。按下搜索字符串后重复 Ctrl+R 以使用相同的字符串向后重复搜索。
You only need to type a command once, then you can recall that command from any substrings of the command. In most cases, you can recall entire very long commands and their various variants with just two to three well-placed search letters. No preconfigurations needed other than using your shell normally and it is self-adaptive to how you used the shell, simply type the full command once and the commands would be automatically added to your command history.
您只需要键入一次命令,然后就可以从该命令的任何子字符串中调用该命令。在大多数情况下,您只需使用两到三个位置合适的搜索字母就可以回忆起整个很长的命令及其各种变体。除了正常使用您的 shell 之外不需要任何预配置,并且它可以自适应您使用 shell 的方式,只需键入一次完整的命令,这些命令就会自动添加到您的命令历史记录中。
git commit --amend
:<Ctrl+R>am
git pull
:<Ctrl+R>pu
git rebase --rebase-merges -i --onto origin/develop origin/develop feature/blue-header
:<Ctrl+R>blu
git rebase --abort
:<Ctrl-R>ab
git rebase --continue
:<Ctrl-R>con
docker-compose stop && git pull && make && docker-compose up -d
:<Ctrl-R>up
- etc
git commit --amend
:<Ctrl+R>am
git pull
:<Ctrl+R>pu
git rebase --rebase-merges -i --onto origin/develop origin/develop feature/blue-header
:<Ctrl+R>blu
git rebase --abort
:<Ctrl-R>ab
git rebase --continue
:<Ctrl-R>con
docker-compose stop && git pull && make && docker-compose up -d
:<Ctrl-R>up
- 等等
Moreover, Ctrl-R works not on just bash, but a lot of programs that uses readline library (and there are a lot of them), like Python shell, IPython, mysql shell, psql shell, irb (ruby), etc.
此外,Ctrl-R 不仅适用于 bash,还适用于许多使用 readline 库的程序(并且有很多),例如 Python shell、IPython、mysql shell、psql shell、irb (ruby) 等。
回答by john01dav
In your example, you compare it to a MySql prompt. The way that works is that a MySql process starts, and you give your commands to that process. As such, why not write something similar in your language of choice? Here's a simple example in C++:
在您的示例中,您将其与 MySql 提示进行比较。其工作方式是启动一个 MySql 进程,然后您向该进程发出命令。因此,为什么不用您选择的语言写一些类似的东西呢?这是 C++ 中的一个简单示例:
#include <iostream>
#include <cstdlib>
int main(int argc, char *argv[]){
while(true){
std::cout << "git> ";
std::cout.flush();
std::string command;
std::getline(std::cin, command);
if(command == "exit") break;
std::system("git " + command);
}
return 0;
}
Please note that I just wrote that from memory and that I didn't check it with a compiler. There may be trivial syntax errors.
请注意,我只是从内存中编写的,并且没有使用编译器进行检查。可能会有一些细微的语法错误。