47
  • I have sublime text as the default editor in git (and it works)
  • git config --edit opens the config file in sublime text (Awesome)

My Question:

What is the command to open say index.html or style.css from inside the project directory?

Basically when I'm working on a project I would like to be able to open a file from git. How to do this. Every tutorial seems to go over merge, clone, commit yes we all know these, how to do this simple command. Or is this not possible from within git?

  • windows 7
  • msysgit version 1.8.0

I understand git is not a launcher and is strictly for version control. Just want to know what options I have with the tools at hand.

The question came up while i was commiting a project and realized i needed to make a small edit to a css file i had closed already and was wondering if i could open to edit the file from within git since i had it up.

Seems this is not possible (not the end of the world) I just like to understand all of my options with the tools i use is all

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Danferth
  • 1,841
  • 5
  • 19
  • 25
  • 1
    I guess my question would be, why? git config --edit is just a convenience function. That's all. It has the same effect as simply opening the file with git. Git is a dvcs. It doesn't do anything with files *while* you're editing them. It manages the revisions of files after you save and commit them. What would be the benefit of 'opening a file with git' to you? Maybe you want to install a sublime text plugin to help work with your projects in git. – Thomas Dignan Dec 15 '12 at 00:45
  • 1
    @Tom Dignan I understand what git is for and use it accordingly. version control and branching projects. The question came up while i was commiting a project and realized i needed to make a small edit to a css file i had closed already and was wondering if i could open to edit the file from within git since i had it up. This isn't something i would do normally. just sense I'm new to git thought maybe there is a way and could not figure it out. If not possible it's not going to crush my world or anything just like to understand all of the options i have with the tools at hand is all. – Danferth Dec 15 '12 at 00:52
  • Mistake in my first comment, I said "It has the same effect as simply opening the file with git" -- replace git with "your favorite editor" – Thomas Dignan Dec 20 '12 at 08:07

21 Answers21

82

Git has nothing to do with how you open/edit files in your project. Configuring the editor in git is only so that git internal things that require an editor (commit messages for example) can use your preferred editor.

If you just want to open files from the command line (cmd.exe) as if they were double clicked in the windows explorer, I think you can use start <filename>.

Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
  • 1
    The question does not have an actual answer it seems (see my edit to question), although you did give me a command line option for essentially double clicking files, so +1, thanks – Danferth Dec 20 '12 at 23:34
  • Just what I was looking for! Thanks a bunch :) I searched for "open" commands, but they did squat. – MartinJH Jun 06 '15 at 11:47
  • A use case is to create other git functionality that works the same as other git commands. I've posted another answer on how this can be done. – eedrah Mar 27 '18 at 02:47
25

I was able to do this by using this command:

notepad .gitignore

And it would open the .gitignore file in Notepad.

Nick Young
  • 359
  • 3
  • 2
10

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
    
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
arbolitoloco
  • 125
  • 2
  • 11
6

Just use the vi + filename command.

Example:

vi stylesheet.css

This will open vi editor with the file content.

To start editing, press I

Asopiah
  • 61
  • 1
  • 1
5

Brenton Alker above said 'start ' works -- I'll add a caveat to that: that works for all files that are already associated with sublime-text (as he says, it works as if they were double clicked in windows explorer).

But if, for example, you wanted to open the .gitignore file from your shell into sublime_text, and .gitignore is not associated with sublime_text, here's what I did:

I edited my PATH environment variable to contain the Sublime Text folder within program files, the one that holds sublime_text.exe. Now, in my terminal (I use powershell, but it works from any terminal), when I type 'sublime_text .gitignore' the .gitignore from my current directory opens in Sublime!

I tried to make a .bat file called sublime.bat that would work so that I could just type sublime .gitignore, but that didn't work - it opened sublime text but not the file for some reason. I'm content with sublime_text (tab completion simplifies it for me, actually -- simply 'su[tab]' does the trick!

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • 2
    This is much better than the accepted answer thanks. I renamed the .exe to sb.exe so i can just type 'sb file.txt' – Luke De Feo Jan 20 '14 at 16:44
4

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.

eedrah
  • 2,245
  • 18
  • 32
3

You can use the git command line as a terminal my dude you just know the commands are bash To create a file

touch file.txt

To open a file

code file.py 
atom file.py
start file.py

ect

To open your current folder and everything inside of it in your text editor

code . 

To make a folder

mkdir folder1 folder2 folder3 

You can make as many as you want at once this works with touch to

2

You can use the following commands to open a file in git bash:

vi <filename>               -- to open a file

i                           -- to insert into the file 

ESC button followed by :wq   -- to save and close the file 

Hope it helps.

Any other terminal based text editor, like vim, nano and many will also do the job just fine.

rajat prakash
  • 189
  • 1
  • 6
1

while you are working in some whatever project and you want to make a minor change you can use git default editor, however you'd probably need a little script that parse the file generated by command below

git config -l

then the variable code.editor holds the value /Applications/Sublime_Text.app -n -w

which you can open using os.system()

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
1

I just downloaded Git 2.7.0 and added an alias to the .bashrc for editing files with VS Code:

alias code='/c/Program\ Files\ \(x86\)/Microsoft\ VS\ Code/bin/code.cmd'

Should also work with other Editors...

stehuebn
  • 11
  • 1
1

I used Atom, to open files this works for me

atom index.html

Hopefully this helps.

dstineback
  • 103
  • 2
  • 4
1

To open a file in Sublime Text,

Here is an example for opening a sample text file in Sublime Text editor

subl <filename>.txt

subl is previously created as an alias name,containing the directory file, using the "echo" command in Git Bash shell.

Arnab Sinha
  • 301
  • 2
  • 7
  • 17
1

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

1

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
wbadry
  • 797
  • 16
  • 27
  • Works perfectly and is really simple. For 64bits use: alias notepad="/c/Program\ Files/Notepad++/notepad++.exe" – pasx Aug 12 '19 at 12:05
0

Maybe could be useful to open an editor from a script shared in a git repository, without assuming which editor could have anyone will use that script, but only that they have git.

Here you can test if editor is set in git config, and also open files not associated with that editor:

alias editor="$(git config core.editor)"
if [ "$(alias editor | sed -r "s/.*='(.*)'/\1/")" != "" ]; then
    editor <filename>
else
    start <filename>
fi

Works great with my .gitconfig on windows:

[core]
    editor = 'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin
Hazzard17
  • 633
  • 7
  • 14
0

OP asked: on Windows command line, how can I open a file in my preferred code editor using a git command? Answer with simplified example:

  1. create a new text file containing one line, such as: notepad++ %1
  2. save the file as cmd type to any folder, such as: c:\myfolder\git-edit.cmd
  3. add an alias: git config --global alias.edit "!c:/myfolder/git-edit.cmd"
  4. use the alias: git edit readme.md

The result of the above is Notepad++ opening the file readme.md

user9375338
  • 102
  • 1
  • 10
0

If you are using git bash as your terminal, you can also use the cat <filename> even inside windows without opening other applications. Currently, I am using git bash inside the terminal preview.

enter image description here

Jay
  • 641
  • 9
  • 11
-1

You must have an application associated with the file type. You must be in the folder that houses the file. In gitbash: start file.extension

Candy
  • 21
  • 6
-1

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.

-1

We would always prefer to use vi -- to open a file

vi <filename> -- to open a file
Pramod Lawate
  • 615
  • 1
  • 7
  • 21
-2

To open a file in git, with windows you will type explorer . , notice the space between explorer and the dot. On mac you can open it with open . , and in Linux with nautilus . , notice the period at the end of each one.

MacroMan
  • 2,335
  • 1
  • 27
  • 36
Sparks
  • 1
  • 1
  • 1
    This will open a file explorer at the current location. Not sure that answers the question. Also note, you can `surround text` in backticks ` to format your code appropriately. – MacroMan Aug 13 '19 at 13:19