How do I make Git use the editor of my choice for commits?

后端 未结 27 2057
庸人自扰
庸人自扰 2020-11-22 01:33

I would prefer to write my commit messages in Vim, but it is opening them in Emacs.

How do I configure Git to always use Vim? Note that I want to do this globally,

相关标签:
27条回答
  • 2020-11-22 01:52

    If you want to set the editor only for Git, do either (you don’t need both):

    • Set core.editor in your Git config: git config --global core.editor "vim"
    • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

    If you want to set the editor for Git and also other programs, set the standardized VISUAL and EDITOR environment variables*:

    export VISUAL=vim
    export EDITOR="$VISUAL"
    

    * Setting both is not necessarily needed, but some programs may not use the more-correct VISUAL. See VISUAL vs. EDITOR.


    For Sublime Text: Add this to the .gitconfig. The --wait is important (it allows to type text in sublime and will wait for save/close event.)

    [core]
        editor = 'subl' --wait
    

    'subl' can be replaced by the full path of the executable but is usually available when correctly installed.

    0 讨论(0)
  • 2020-11-22 01:52

    In windows 7, while adding the "Sublime" editor it was still giving me an error:

    Aborting commit due to empty commit message.

    Sublime was not able to keep the focus.

    To fix this I opened the .gitconfig file in c:/users/username/ folder and added the following line with --wait option outside the single quotes.

    [core]
          editor = 'F:/Program Files/Sublime Text 2/sublime_text.exe' --wait
    

    Hope its helpful to somebody facing similar issue with Sublime.

    0 讨论(0)
  • 2020-11-22 01:53

    For Windows users who want to use neovim with the Windows Subsystem for Linux:

    git config core.editor "C:/Windows/system32/bash.exe --login -c 'nvim .git/COMMIT_EDITMSG'"

    This is not a fool-proof solution as it doesn't handle interactive rebasing (for example). Improvements very welcome!

    0 讨论(0)
  • 2020-11-22 01:54

    For users of TextWrangler from the Mac app store:

    git config --global core.editor "open -n -W -a TextWrangler"
    

    Also, make sure your "TextWrangler > Preferences > Application > When TextWrangler becomes active:" setting is set to "Do nothing"

    This works for me on OS X 10.11.4 with TextWrangler 5.0.2 from the Mac app store.

    Explanation:

    The -n means open in a new instance.

    The -W means to wait until the application exits before using the contents of the edited file as the commit message.

    The -a TextWrangler means use the TextWrangler application to open the file.

    See man open in your Mac Terminal app for more details.

    0 讨论(0)
  • 2020-11-22 01:55

    Best settings for Sublime Text 3 as your Git editor (Windows & Linux instructions):

    To follow these instructions in Windows make sure you have installed Git for Windows. In Windows, I like to use Git Bash so that it feels more like Linux.

    First, we want to create a special Sublime Text project so that we can specify special project settings we want set whenever Git calls the editor, to make things easier when editing in Git. For example, I normally set my ruler to 120 chars in most projects, but for Git commit messages I want it to be 72 characters so that it fits nicely in a terminal when you call git log or git lg.


    1. Create a Sublime Text project with settings we want to use to edit Git commit messages

    Open Sublime Text and go to menu "File""New Window" to create a new anonymous project. Go to menu "Project""Save Project As..." and choose a place to save it. In Linux I saved it in my Linux home directory with the file name .gitconfig.sublime-project. Its path is therefore: ~/.gitconfig.sublime-project. In Windows also save it in your home directory, for example: C:\Users\MY_USER_NAME\.gitconfig.sublime-project Now go to menu "Project""Edit Project" to edit the project settings. Paste the following and save the settings. Make any further edits for your project settings if desired.

    {
        // For folder settings help see here: https://www.sublimetext.com/docs/3/projects.html
    
        "folders":
        [
    
        ],
    
        "settings":
        {
    
            // Disables horizontal scrolling if enabled.
            // May be set to true, false, or "auto", where it will be disabled for
            // source code, and otherwise enabled.
            "word_wrap": false,
    
            // Set to a value other than 0 to force wrapping at that column rather than the
            // window width
            "wrap_width": 0,
    
            // Columns in which to display vertical rulers
            "rulers": [72, 50], //72 is recommended by git for commit message content, and 50 for commit titles
    
            // The number of spaces a tab is considered equal to
            "tab_size": 4,
    
            // Set to true to insert spaces when tab is pressed
            "translate_tabs_to_spaces": true,
        },
    
        "build_systems":
        [
    
        ]
    
    }
    

    2. Set the editor to be used by Git

    Now we need to set the editor to be used by Git, by editing the .gitconfig file.

    For Linux:

    Your user copy of this will be located in ~/.gitconfig. Open this file and add the following lines. Be sure to use the proper path name to the Git project you just created above! I'm using ~/.gitconfig.sublime-project.

    [core]
        editor = subl --project ~/.gitconfig.sublime-project --wait
    

    The --wait is important, as it forces Git to wait until you close the file before it continues on. The --project line is important to tell Sublime Text which project you want opened whenever Git opens Sublime Text.

    Per @digitaldreamer's answer above (https://stackoverflow.com/a/2596835/4561887), "subl can be replaced by the full path of the executable but [the alias subl] is usually available when [Sublime is] correctly installed."

    For Windows:

    For Windows, first read the Linux instructions for background information. Now we will do something almost identical.

    (OPTIONAL: create a subl alias for use in Git Bash):

    Open up a text editor (for example, Notepad, Notepad++, Sublime Text, Geany, etc.), and create a file called ".bash_profile" in your home directory. Its path will therefore be: C:\Users\MY_USER_NAME\.bash_profile. Save the following into it:

    alias subl="/c/Program\ Files/Sublime\ Text\ 3/subl.exe"
    

    This creates a Git Bash alias called subl that we can now use in Git Bash for Windows, to easily open Sublime Text. This step isn't required, but it's useful for general Git Bash use. Now you can call subl ., for instance, in Git Bash to open up a new Sublime Text project in your current directory.

    (MANDATORY):

    Edit the .gitconfig file found in your home directory: C:\Users\MY_USER_NAME\.gitconfig, by adding the following to it. Notice the subtle changes from the Linux instructions above:

    [core]
      editor = 'C:/Program Files/Sublime Text 3/subl.exe' --project ~/.gitconfig.sublime-project --wait
    
    • Notice that you must specify the full path to the Sublime Text executable. Note the direction of the slashes! Use / NOT \ to separate folders in the path name! (Thanks VonC for helping me see this).
    • Our subl alias we made for Git Bash above doesn't work here, so you can't use it like we did in the Linux example, instead you must specify the whole path as shown above.
    • The ~ symbol, however, does still work here to get to your Windows home directory.

    2.5. (Optional) Install the "Git" package into Sublime Text 3.

    This gives you syntax highlighting for git commit messages, as well as access to other Git commands such as git blame (which I use frequently in Sublime Text) or git commit (which I don't use in Sublime Text since I'd prefer the command-line for general Git flow, as I've mentioned in my comments below this answer).

    To install a package: First, ensure “Package Control” is installed. Next, press Ctrl + Shift + P (same as Tools → Command Palette) and type all or part of “Package Control: Install Package”, then press Enter. In the search box that comes up, search for the package "Git" and hit Enter on it, or click on it, to automatically install it.

    Once installed, Ctrl + Shift + P then searching for "git" will bring up Git commands you can use internally inside Sublime Text now, such as git blame.


    3. Use it

    Now when you call git commit, for instance, as normal from the command-line, Sublime Text will open up into the .gitconfig.sublime-project we created above, with that project's settings! As you type a paragraph you'll notice it extends past the ruler we set since soft word-wrap is off. To force hard wrap via auto-inserted hard-returns at the end of each line, put your cursor on the long line you want auto-wrapped and press Alt + Q. It will now hard-wrap/hard-fold at 72 characters, which is what we set in the project settings' "rulers" parameter above.

    Now, save your commit message with Ctrl + S, and exit (to complete your git commit) with Ctrl + Shift + W.

    Done!

    Related:

    1. Git mergetool with Meld on Windows
    2. https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
    0 讨论(0)
  • 2020-11-22 01:55

    Just because I came here looking for a one-time solution (in my case, I usually use vim but this one time I wanted to use VS Code) for a single command and others might want to know as well:

    GIT_EDITOR='code -w' git rebase -i …
    

    Here's my git/hub version just for context:

    git version 2.24.2 (Apple Git-127)
    hub version 2.14.1
    
    0 讨论(0)
提交回复
热议问题