By Nocturnal


2019-06-08 09:11:16 8 Comments

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.

13 comments

@akash 2019-06-29 07:20:49

Use a brackets editor, it's easy to use your code and git commands, it also has many features.

enter image description here

To the top right corner the second binocular icon is used to install extensions.

enter image description here

Search extension brackets git like the above image and install it.

enter image description here

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.

@Adamsan 2019-06-17 22:23:16

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.

@Umur Kontacı 2019-06-10 19:54:25

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

@Sergiy Kolodyazhnyy 2019-06-10 23:22:47

Decent idea, however there's two problems. One, the ; after do leads to bash: syntax error near unexpected token ;'` Second, the eval part is prone to vulnerabilities. For example, consider what happens if I type status;cat /etc/passwd into this small shell. Harmless example, but you get the idea what can happen .You can simplify that into while IFS= read -r -p "git> " gitcmd; do [ "x$gitcmd" != "x" ] && git "$gitcmd";done This of course is not bullet proof, but 1 - it is simpler and 2 - avoids non-git command execution (by virtue of double quotes ). Not ideal, just a little better

@Sergiy Kolodyazhnyy 2019-06-14 15:55:13

@Abigail Funny :) Why yes, shell itself isn't the most secure application. Even catting a file can be a problem. However, the point I was originally making is that we don't see mysql interpreter executing shell commands, not without system or \! at the beginning at least. Where the assumption might be that this "repl" executes git commands only, it in fact allows more than that.

@Sergiy Kolodyazhnyy 2019-06-16 00:49:42

@Abigail I am not afraid, because I know what this does. Others who don't - may end up breaking something they shouldn't. And again - eval isn't the best idea for running commands, especially when user-controlled input is involved.

@Umur Kontacı 2019-06-17 03:53:52

Fixed the typo after do. Thanks! re: security: This is obviously not meant to be distributed as a separate application. Just put in your .bash_profile if you don't like typing git. I'm not too worried about it being "insecure". If you paste stuff in your bash that you don't understand (including this oneliner script), you're gonna have a bad time. I like this because it's simple, easy to write and easy to read, and easy to improve upon.

@LogicalBranch 2019-06-10 15:00:11

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 become git git init)

Example commands:

  1. init
  2. add .
  3. stage .
  4. commit -m "inital commit"
  5. %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.

@prosti 2019-06-10 16:08:39

Use your editor.

Type the command like commit from your favorite editor like vs code and be more efficient with git:

enter image description here

Or type git to get all the commands:

enter image description here

@Glen Pierce 2019-06-10 18:16:21

I'm surprised by all these down votes. This isn't a terrible answer for people who use IDEs that support these features.

@LogicalBranch 2019-06-10 18:45:47

I think people have down-voted because not everyone uses/likes VS-Code. Either way, I think it's a decent solution so +1 from me.

@prosti 2019-06-10 19:19:48

@LogicalBranch, people are generally using the git from command line, and I am aware of that, but the git support inside some editors exists and it is worth trying.

@CoffeeTableEspresso 2019-06-10 23:48:28

I don't like this answer because not everyone uses VS code (I personally don't like it), but won't downvote because this is a nice solution for ppl that do use it.

@prosti 2019-06-11 04:43:39

@CoffeeTableEspresso, if you are using sublime there is a plugin called gitsavvy, and so on... Almost every editor nowadays has some kind of support for git. This was the point of the answer, as you can read "Use your editor".

@Ian Ringrose 2019-06-11 12:28:27

This mostly comes down to if someone uses many editors on the same os, or the same editor on many different os.

@CoffeeTableEspresso 2019-06-11 16:55:43

@prosti I either lots of different editors for different things (I use 2 or 3 different editors each day), or I use vim, so I just end up using the terminal since it's easier to avoid messing something up that way.

@CoffeeTableEspresso 2019-06-11 16:56:09

@IanRingrose I end up using different editors on different OSes.

@Sam Weaver 2019-06-10 07:11:40

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

@Sergiy Kolodyazhnyy 2019-06-10 23:34:36

OK, I've already mentioned that on Umur's answer, but the use of eval in the original script source isn't the best idea. Tell your friend to use while IFS= read -r -p "git> " gitcmd; do [ "x$gitcmd" != "x" ] && git "$gitcmd";done instead

@Lie Ryan 2019-06-10 02:02:58

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.

@JacobIRR 2019-06-10 01:46:59

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'

@qwr 2019-06-10 15:55:39

where's commit :P

@JacobIRR 2019-06-10 17:19:49

I don't include commit or push since I want a few extra seconds (while typing) to be sure that I'm not destroying something

@qwr 2019-06-10 18:21:10

Commit and push shouldn't be able to destroy anything unless you use force push. But I try to use git status beforehand.

@CoffeeTableEspresso 2019-06-10 23:50:56

this is what I've done as well, +1. Although I'm tempted to try gitsh as other answers have mentioned

@LogicalBranch 2019-06-11 10:53:44

@CoffeeTableEspresso I respect the fact that you acknowledge that a few keystrokes is the difference between saving updates and destroying a project.

@Emile Vrijdags 2019-06-17 13:33:43

If you use oh-my-zsh there is a git plugin with a bunch of these predefined (can maybe be used for inspiration): github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/…

@john01dav 2019-06-09 17:06:37

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.

@Thomas Weller 2019-06-09 18:23:13

Just my thought. Anyone on Stack Overflow should be able to code such a program himself. The programming language does not really matter.

@john01dav 2019-06-09 21:24:06

@ThomasWeller I definitely agree. I posted the program to precisely show what I was talking about, not because it is a difficult program to write.

@nomadictype 2019-06-09 22:51:57

You're going to invest a lot of time with this approach if you want the program to be bug-free and have a decent amount of features. For example, after fixing the initial build failure (std::system() wants const char*) you'll notice that there is an infinite loop on EOF. You might want history/readline support, tab completion, some builtins to change directory / set env vars / shell out / ..., etc. If there is existing software (like gitsh in this case), why not use it?

@john01dav 2019-06-09 22:57:58

@nomadictype That's a valid criticism, but learning other software is also a time commitment. The advantages with this approach are that only a few minutes are needed to get it working, and thst it will do exactly what you expect or want (with changes).

@Lie Ryan 2019-06-11 01:20:22

The loss of readline, line editing, history support, being able to run simple commands like ls, etc is going to cost you a lot more than the four keystrokes or so you saved with this.

@nomadictype 2019-06-09 14:06:36

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 ".

@TheOnlyMrCat 2019-06-10 06:17:30

Note that if you're using another shell, you'll have to put it in the appropriate file. For example, for zsh, you'll put it in ~/zshrc, for tcsh, you'll put it in ~/tcshrc, etc.

@Thomas 2019-06-08 09:36:15

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.

@Tim Biegeleisen 2019-06-08 09:39:06

The drawback of this approach is that a separate alias would need to be created for every Git command.

@Thomas 2019-06-08 09:42:27

Only for the most frequently used. I mean, how often do you use git hash-object or git interpret-trailers? I'm just offering this as an alternative because as far as I know, what the question is asking for doesn't really exist.

@rkta 2019-06-08 18:26:45

In addition to ci for commit I also use a shell alias g for git, this reduces most of the typing and lets me stay in my preferred shell.

@user8549967 2019-06-08 20:50:36

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.

@ruohola 2019-06-10 07:02:32

This really is most similar to what OP is asking for, and in a very lightweight package!

@Konrad Rudolph 2019-06-10 13:23:43

This would be perfect if it worked with readline but unfortunately it doesn’t (not surprisingly since this is strictly a hack around Perl’s -ne flags).

@user8549967 2019-06-10 17:35:28

@KonradRudolph perl -MTerm::ReadLine -E '$n = Term::ReadLine -> new ("git"); while ($_ = $n -> readline ("git > ")) {system "git $_"}'

@alfunx 2019-06-08 10:29:32

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.

@Kevin 2019-06-09 02:38:05

grumble... repl(1) is not a REPL. It doesn't eval or print anything. It runs programs.

@Yakk - Adam Nevraumont 2019-06-10 19:26:57

@Kevin It reads in user requests, evaluate the user request (by running a program), and prints the output of the program. This is also what shells do.

@Kevin 2019-06-10 19:29:24

@Yakk-AdamNevraumont: No, it most certainly does not "print the output of the program." It hooks the program's stdout up to the terminal, and then the program prints its own output - except the program's stdout is already hooked up to the terminal (inherited automatically over the fork()/exec()), so repl(1) isn't even doing that.

@leftaroundabout 2019-06-11 08:25:33

@Kevin I understand your sentiment, however the strict definition of a REPL would exclude most interpreter “REPL”s in various languages. Only in pure functional languages could you ensure that the evaluation has no side-effects, but even in Haskell the “REPL” GHCi will by default also accept IO actions, and execute them including side-effects such as printing to the terminal-connected screen.

@Kevin 2019-06-11 15:15:05

@leftaroundabout: I never said anything about side effects. I just said that a read, eval, print loop should actually read, eval, and print.

@Kyle Strand 2019-06-11 17:12:30

@Kevin repl isn't a standalone REPL; it's a means of creating a REPL system in which repl is only the interactive component. Is that fair?

@Kevin 2019-06-11 17:38:23

@Kyle: I didn't say it had to be standalone either. Why is everyone so intent on putting words in my mouth?

@Kyle Strand 2019-06-11 18:14:26

@Kevin I wasn't trying to rephrase what you said, merely to provide an explanation of what repl is and why it's named that way that you might agree with. I take it you don't agree with the "REPL system in which repl is only the interactive component" explanation, then? Why not?

@Claude Brisson 2019-06-11 22:01:54

I wonder why no one did came with gisht as project name. It means finger in Albanian.

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How do I check out a remote Git branch?

35 Answered Questions

[SOLVED] How do I discard unstaged changes in Git?

  • 2008-09-09 19:33:59
  • Readonly
  • 2357397 View
  • 4468 Score
  • 35 Answer
  • Tags:   git version-control

39 Answered Questions

[SOLVED] How do I delete a Git branch locally and remotely?

33 Answered Questions

[SOLVED] How do I undo 'git add' before commit?

41 Answered Questions

[SOLVED] How do I revert a Git repository to a previous commit?

42 Answered Questions

[SOLVED] How do I force "git pull" to overwrite local files?

80 Answered Questions

[SOLVED] How do I undo the most recent local commits in Git?

32 Answered Questions

[SOLVED] How do I rename a local Git branch?

  • 2011-07-06 03:20:36
  • Forrest
  • 2597592 View
  • 7883 Score
  • 32 Answer
  • Tags:   git git-branch

35 Answered Questions

[SOLVED] How to remove local (untracked) files from the current Git working tree

  • 2008-09-14 09:06:10
  • Readonly
  • 2101801 View
  • 6455 Score
  • 35 Answer
  • Tags:   git branch git-branch

34 Answered Questions

[SOLVED] How to resolve merge conflicts in Git

Sponsored Content