How do I avoid typing “git” at the begining of every Git command?

后端 未结 15 792
醉酒成梦
醉酒成梦 2021-01-29 22:31

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

15条回答
  •  广开言路
    2021-01-29 22:48

    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.

提交回复
热议问题