Command to open file with git

后端 未结 18 1289
滥情空心
滥情空心 2021-01-30 04:05
  • I have sublime text as the default editor in git (and it works)
  • git config --edit opens the config file in sublime text (Awesome)
相关标签:
18条回答
  • 2021-01-30 05:02

    A simple solution to the problem is nano index.html and git or any other terminal will open the file right on the terminal, then you edit from there.

    You see commands at the bottom of the edit page on how to save.

    0 讨论(0)
  • 2021-01-30 05:03

    I know this is ancient, but I need to do this at the end of a git helper script (alias that creates aliases from a template), and found what I think is the real answer:

    There is a porcelain-ish helper called git-sh-setup that, when sourced, gives you a git_editor function that

    runs an editor of user’s choice (GIT_EDITOR, core.editor, VISUAL or EDITOR) on a given file, but error out if no editor is specified and the terminal is dumb.

    The git-sh-setup documentation description basically tells you not to use it, and that's probably good advice in this case.

    Fortunately, the git-sh-setup is a shell script and the git_editor portion of it is pretty small, and we can just copy that:

    git_editor() {
        if test -z "${GIT_EDITOR:+set}"
        then
            GIT_EDITOR="$(git var GIT_EDITOR)" || return $?
        fi
        eval "$GIT_EDITOR" '"$@"'
    }
    

    You should be able to put that in your own scripts or turn it into a bash alias and then call it like git_editor file1.txt file2.txt ...

    0 讨论(0)
  • 2021-01-30 05:04

    I found a workaround for this via this link. In a nutshell, you have to:

    1. Create a file named subl (or any name you'd like for the command to call for Sublime Text) without extension
    2. Put this command inside the above file(Replace the path for executable if necessary):

      #!/bin/sh
      "C:\Program Files\Sublime Text 2\sublime_text.exe" $1 &
      
    3. Place that subl file inside the adequate command directory according to your OS and Sublime Text version (If you have doubts, check the above link comments section). In my case, I'm using Sublime Text 3 with Windows 10 64bit so I placed it in:

      C:\Program Files (x86)\Git\usr\bin
      
    4. Now, in order for you to open the desired file, in git bash use (within file folder)

      subl filename
      
    0 讨论(0)
  • 2021-01-30 05:04

    You can create an alias to open a file in your default editor by appending the following line to your .gitconfig file:

    edit = "!f() { $(git config core.editor) -- $@; }; f"
    

    Then, git edit foo.txt will open the file foo.txt for editing.

    It's much easier to open .gitconfig with git config --global --edit and paste the line, rather than figure out how to escape all the characters to enter the alias directly from the command line with git config alias.edit "..."

    How it works

    • ! starts a bash command, not an internal git command
    • f() {...}; starts a function
    • $(git config core.editor) will get the name of your editor, from the local config, or the global if the local is not set. Unfortunately it will not look in $VISUAL or $EDITOR for this, if none is set.
    • -- separates the editor command with the file list. This works for most command line editors, so is safer to put in. If skipped and the core.editor is not set then it is possible that an executable file is executed instead of being edited. With it here, the command will just fail.
    • $@ will add the files entered at the command line.
    • f will execute the function after it is defined.

    Use case

    The other answers express doubt as to why you would want this. My use case is that I want to edit files as part of other git functions that I am building, and I want to edit them in the same editor that the user has configured. For example, the following is one of my aliases:

    reedit = "!f() { $(git config core.editor) -- $(git diff --name-only $1); }; f"
    

    Then, git reedit will open all the files that I have already started modifying, and git reedit --cached will open all the staged files.

    0 讨论(0)
  • Assuming you are inside your repository root folder

    alias notepad="/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"
    

    then you can open any file with Notepad++ with:

    notepad readme.md
    
    0 讨论(0)
  • 2021-01-30 05:05

    Just use the vi + filename command.

    Example:

    vi stylesheet.css
    

    This will open vi editor with the file content.

    To start editing, press I

    0 讨论(0)
提交回复
热议问题