How do I avoid typing "git" at the begining of every Git command?
GitVersion ControlCommand Line-InterfaceGit BashGit Problem Overview
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 git
command only once in the beginning after opening a command prompt to get into "Git mode".
For example:
git>
After which every command we type is by default interpreted as a Git command.
In a way similar to how we use the MySQL shell to write database commands:
mysql>
This will save me from having to type git
hundreds of times a day.
NOTE: I'm using git-bash
, on Windows.
Git Solutions
Solution 1 - Git
You might want to try gitsh. From their readme:
> The gitsh
program is an interactive shell for git. From within gitsh
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$ >
Or have a look at the other projects linked there:
> * git-sh - A customised bash shell with a Git prompt, aliases, and completion. > * gitsh - A simple Git shell written in Perl. > * repl - Wraps any program with subcommands in a REPL.
Note: Haven't used this myself.
Solution 2 - Git
A Perl one-liner which will do this:
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
.
Solution 3 - Git
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:
alias commit='git commit'
alias checkout='git checkout'
...
Also note that you can create aliases within Git itself:
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.
Solution 4 - Git
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:
# 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'
Solution 5 - Git
Use your editor.
Type the command like commit
from your favorite editor like vs code and be more efficient with git:
Or type git
to get all the commands:
Solution 6 - Git
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
Solution 7 - Git
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 ".
Solution 8 - Git
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.
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
)
Example commands:
init
add .
stage .
commit -m "inital commit"
%run; %exit
Additional information (for Linux users):
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
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.
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.
Solution 9 - Git
Another approach that will work with any commands: use Ctrl+R (reverse-i-search).
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.
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.
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
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.
Solution 10 - Git
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.
Solution 11 - Git
For basic stuff, you can do:
function ggit(){ while true; do printf 'git> '; read; eval git $REPLY; done }
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>
Exit with ctrl+c
Solution 12 - Git
When I used Windows 7 with Conemu, I added the following to my dev environment startup script:
doskey g=git $*
With this, I could just use the g
command instead of typing git
.
Last I tried with Windows 10 and Conemu, it did not work, there is a bug, I think, but it's worth a try.
Solution 13 - Git
Use a brackets editor, it's easy to use your code and git commands, it also has many features.
To the top right corner the second binocular icon is used to install extensions.
Search extension brackets git
like the above image and install it.
Again to the top right corner there will show the fourth icon, so just click and see the changes like the above image.
If you want to install brackets, use the following commands:
sudo add-apt-repository ppa:webupd8team/brackets
sudo apt-get update
sudo apt-get install brackets
For more information, you can read: How to Install Brackets Code Editor in Ubuntu and Linux Mint on Ubuntupit.
Solution 14 - 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.
Solution 15 - Git
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.