Noc*_*nal 191 git version-control command-line-interface git-bash
我想知道是否有一种方法可以避免git在每个Git命令的开头都键入单词。
如果有一种方法可以git在打开命令提示符进入“ Git模式”后仅在开始时使用一次命令,那将是很好的。
例如:
git>
Run Code Online (Sandbox Code Playgroud)
之后,默认情况下,我们键入的每个命令都将解释为Git命令。
与我们使用MySQL Shell编写数据库命令的方式类似:
mysql>
Run Code Online (Sandbox Code Playgroud)
这将使我不必git每天都要键入数百次。
注意:我git-bash在Windows上使用。
alf*_*unx 190
您可能要尝试gitsh。从他们的自述文件:
该
gitsh程序是git的交互式shell。从内部gitsh您可以发出任何git命令,甚至可以使用本地别名和配置。
- Git命令倾向于成组出现。
git通过在专用的git shell中运行它们,避免一遍又一遍地输入:Run Code Online (Sandbox Code Playgroud)sh$ gitsh gitsh% status gitsh% add . gitsh% commit -m "Ship it!" gitsh% push gitsh% ctrl-d sh$
或查看其中链接的其他项目:
注意:我自己没有使用过。
小智 115
一个Perl单行代码将执行以下操作:
perl -nE 'BEGIN {print "git > "} system "git $_"; print "git > "'
Run Code Online (Sandbox Code Playgroud)
这将执行您键入的任何前缀为的命令git。并且它将一直这样做,直到您击中^D。
Tho*_*mas 57
这并不完全是您要的,但是您可以在您~/.bashrc最常用的Git命令中设置一些shell别名:
alias commit='git commit'
alias checkout='git checkout'
...
Run Code Online (Sandbox Code Playgroud)
还要注意,您可以在Git本身中创建别名:
git config --global alias.ci commit
git config --global alias.co checkout
...
Run Code Online (Sandbox Code Playgroud)
这使您可以输入git ci而不是git commit,依此类推。
Jac*_*IRR 32
我非常喜欢在GitBash的〜/ .bash_profile中使用别名。如果您采用这种方法,以下是我的一些最爱:
# 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'
Run Code Online (Sandbox Code Playgroud)
pro*_*sti 31
使用您的编辑器。
commit从喜欢的编辑器(如vs代码)中键入命令,并使用git更加有效:
或键入git以获取所有命令:
Sam*_*ver 26
我的一个朋友制作了一个小的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
Run Code Online (Sandbox Code Playgroud)
nom*_*ype 23
这是另一种方式。它也不是所要求的,但是我已经使用了一段时间了,它非常不错。将以下行添加到您的~/.bashrc:
complete -E -W git
Run Code Online (Sandbox Code Playgroud)
现在在空的Bash提示符下按Tab键将键入“ git”。
Log*_*nch 21
我知道这是一个很晚的答案,但是这个问题确实引起了我的注意,因为我已经很长时间以来一直在处理这种重复的痛苦。
我不确定您的身份,但老实说我不想(我重复DO N'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)
Run Code Online (Sandbox Code Playgroud)
NoGit是一个简单的python脚本,可以防止不必要的重复“ git”关键字。
%undo命令从堆栈中删除最后一个命令%run命令在堆栈中运行命令并清除堆栈%exit命令不执行任何操作即可关闭CLIctr+c与跑步相同%run; %exitgit.history与脚本位于同一文件夹中的文件中git在命令的开头使用关键字,脚本不会重复该关键字(例如: git init不会成为git git init)initadd .stage .commit -m "inital commit"%run; %exit如果需要,可以使用以下方法删除.py扩展并将其转换为可执行文件:
mv ./git.py ./git
chmod +x ./git
Run Code Online (Sandbox Code Playgroud)
然后,不要像这样调用脚本:
python3 git.py
Run Code Online (Sandbox Code Playgroud)
您可以运行以下命令:
./git
Run Code Online (Sandbox Code Playgroud)
如果您懒惰并且不想键入a,./则可以将此脚本移至/bin/文件夹并为其创建别名。
如果你真的,真的懒,请使用以下命令:
sudo cp ./git /bin/nogit
sudo chmod +x /bin/nogit
alias nogit='/bin/nogit'
Run Code Online (Sandbox Code Playgroud)
如果你真的,真的,真的很懒惰,复制并粘贴下面的一行:
sudo cp ./git /bin/nogit && sudo chmod +x /bin/nogit && alias nogit='/bin/nogit'
Run Code Online (Sandbox Code Playgroud)
如果您的懒惰程度达到了人类以前所不知道的水平,那么以下是同一单线的更紧凑的版本:
sudo cp ./git /bin/nogit;sudo chmod +x /bin/nogit;alias nogit='/bin/nogit'
Run Code Online (Sandbox Code Playgroud)
祝好运。
Lie*_*yan 17
适用于任何命令的另一种方法:使用Ctrl + R(反向搜索)。
通过反向搜索,您可以搜索命令历史记录。按下搜索字符串后,请重复Ctrl + R,以再次搜索相同的字符串。
您只需要键入一次命令,然后就可以从该命令的任何子字符串中调用该命令。在大多数情况下,您只需两到三个放置适当的搜索字母就可以调用整个非常长的命令及其各种变体。除了正常使用外壳程序之外,不需要任何预配置,并且它是您使用外壳程序的自适应方式,只需键入一次完整命令,命令便会自动添加到命令历史记录中。
git commit --amend: <Ctrl+R>amgit pull: <Ctrl+R>pugit rebase --rebase-merges -i --onto origin/develop origin/develop feature/blue-header: <Ctrl+R>blugit rebase --abort: <Ctrl-R>abgit rebase --continue: <Ctrl-R>condocker-compose stop && git pull && make && docker-compose up -d: <Ctrl-R>up而且,Ctrl-R不仅适用于bash,而且适用于使用readline库的许多程序(并且有很多),例如Python shell,IPython,mysql shell,psql shell,irb(ruby)等。
joh*_*dav 15
在您的示例中,将其与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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我只是从内存中写的,没有使用编译器进行检查。可能有一些琐碎的语法错误。
Umu*_*acı 13
对于基本内容,您可以执行以下操作:
function ggit(){ while true; do printf 'git> '; read; eval git $REPLY; done }
Run Code Online (Sandbox Code Playgroud)
git> status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: yarn.lock
no changes added to commit (use "git add" and/or "git commit -a")
git> add .
git> status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: yarn.lock
git>
Run Code Online (Sandbox Code Playgroud)
用ctrl + c退出