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?
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
This question is related to
git
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...
I was able to do this by using this command:
notepad .gitignore
And it would open the .gitignore file in Notepad.
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.
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.
I found a workaround for this via this link. In a nutshell, you have to:
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 &
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
Now, in order for you to open the desired file, in git bash use (within file folder)
subl filename
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
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.
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 ...
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 "..."
!
starts a bash command, not an internal git commandf() {...};
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.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.
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
Just use the vi
+ filename
command.
Example:
vi stylesheet.css
This will open vi editor with the file content.
To start editing, press I
I used Atom, to open files this works for me
atom index.html
Hopefully this helps.
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
We would always prefer to use vi -- to open a file
vi <filename> -- to open a file
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()
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!
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
Source: Stackoverflow.com