I\'m wondering if there\'s a way to avoid having to type the word git
at the beginning of every Git command.
It would be nice if there was a way to use the
I know this is a very late answer but this question really struck a note with me because I've been dealing with suffering 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 NoGit to solve this problem:
#!/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)
NoGit is a simple python script to prevent the unnecessary repetition of the "git" keyword.
%undo
command removes the last command from the stack%run
command runs the commands in the stack and clears the stack%exit
command closes the CLI without doing anythingctr+c
is the same as running %run; %exit
git.history
in the same folder as the scriptgit
in the beginning of the command and the script won't duplicate it (E.G: git init
doesn't become git git init
)init
add .
stage .
commit -m "inital commit"
%run; %exit
If you want you can remove the .py
extension and convert it into an executable using:
mv ./git.py ./git
chmod +x ./git
Then instead of calling the script like this:
python3 git.py
You'd run this instead:
./git
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.
If you're really, really lazy, use the following commands:
sudo cp ./git /bin/nogit
sudo chmod +x /bin/nogit
alias nogit='/bin/nogit'
If you're really, really, really lazy, 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.
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
:
complete -E -W git
Now pressing Tab at an empty Bash prompt will type out "git ".
After
while read -erp "*git*${PS1@P}" cmd rest; do
if _=`git help $cmd 2>&-`
then eval git $cmd "$rest"
else eval $cmd "$rest"
fi
done
every command we type is by default interpreted as a Git command
if it looks like one, otherwise it'll be interpreted as-is, so you can intermix git with other commands, and if you want to use a punned command just prefix it with a backslash, rm foo
would be eval'd as git rm foo
, but \rm foo
would run the plain rm
command. ^d out as usual to end it.
Since today: GitHub CLI is available.
GitHub CLI brings GitHub to your terminal. It reduces context switching, helps you focus, and enables you to more easily script and create your own workflows. Earlier this year, we announced the beta of GitHub CLI. Since we released the beta, users have created over 250,000 pull requests, performed over 350,000 merges, and created over 20,000 issues with GitHub CLI. We’ve received so much thoughtful feedback, and today GitHub CLI is out of beta and available to download on Windows, macOS, and Linux.
A friend of mine made a small bash script that accomplishes this. It's called 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
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++:
#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.