git 用git打开文件的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13888223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:06:06  来源:igfitidea点击:

Command to open file with git

git

提问by Danferth

  • I have sublime text as the default editor in git (and it works)
  • git config --editopens the config file in sublime text (Awesome)
  • 我有 sublime text 作为 git 中的默认编辑器(它可以工作)
  • git config --edit以崇高的文本打开配置文件(真棒)

My Question:

我的问题:

What is the command to open say index.htmlor style.cssfrom inside the project directory?

打开index.htmlstyle.css从项目目录中打开的命令是什么?

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?

基本上,当我在做一个项目时,我希望能够从 git 打开一个文件。这该怎么做。每个教程似乎都在讨论合并、克隆、提交,是的,我们都知道这些,如何执行这个简单的命令。或者这在 git 中是不可能的?

  • windows 7
  • msysgit version 1.8.0
  • Windows 7的
  • msysgit 版本 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.

我知道 git 不是启动器,而是严格用于版本控制。只是想知道我手头的工具有哪些选择。

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.

当我提交一个项目并意识到我需要对我已经关闭的 css 文件进行小的编辑时出现了这个问题,并且想知道我是否可以在 git 中打开以编辑该文件,因为我已经有了它。

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

似乎这是不可能的(不是世界末日)我只是想通过我使用的工具了解我的所有选择

回答by Brenton Alker

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.

Git 与您在项目中打开/编辑文件的方式无关。在 git 中配置编辑器只是为了让需要编辑器的 git 内部事物(例如提交消息)可以使用您首选的编辑器。

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

如果您只想从命令行(cmd.exe)打开文件,就像在 Windows 资源管理器中双击它们一样,我认为您可以使用start <filename>.

回答by Nick Young

I was able to do this by using this command:

我可以通过使用以下命令来做到这一点:

notepad .gitignore

And it would open the .gitignore file in Notepad.

它会在记事本中打开 .gitignore 文件。

回答by arbolitoloco

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"  &
    
  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
    
  1. 创建一个名为 subl 的文件(或任何您希望命令调用 Sublime Text 的名称)而没有扩展名
  2. 将此命令放在上述文件中(如有必要,请替换可执行文件的路径):

    #!/bin/sh
    "C:\Program Files\Sublime Text 2\sublime_text.exe"  &
    
  3. 根据您的操作系统和 Sublime Text 版本将该 subl 文件放在适当的命令目录中(如果您有疑问,请查看上面的链接评论部分)。就我而言,我将 Sublime Text 3 与 Windows 10 64 位一起使用,因此我将其放置在:

    C:\Program Files (x86)\Git\usr\bin
    
  4. 现在,为了让您打开所需的文件,在 git bash 中使用(在文件夹中)

    subl filename
    

回答by TKoL

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

Brenton Alker 上面说的 'start' 有效——我会添加一个警告:它适用于已经与 sublime-text 关联的所有文件(正如他所说,它就像在 Windows 资源管理器中双击它们一样)。

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:

但是,例如,如果您想将 shell 中的 .gitignore 文件打开到 sublime_text 中,并且 .gitignore 与 sublime_text 无关,那么我就是这样做的:

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!

我编辑了我的 PATH 环境变量以包含程序文件中的 Sublime Text 文件夹,该文件夹包含 sublime_text.exe。现在,在我的终端中(我使用 powershell,但它可以在任何终端上运行),当我输入“sublime_text .gitignore”时,我当前目录中的 .gitignore 在 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!

我试图制作一个名为 sublime.bat 的 .bat 文件,它可以工作,这样我就可以只输入 sublime .gitignore,但这没有用 - 它由于某种原因打开了 sublime 文本而不是文件。我对 sublime_text 感到满意(实际上,选项卡完成对我来说简化了它——简单的 'su[tab]' 就可以了!

回答by Asopiah

Just use the vi+ filenamecommand.

只需使用vi+filename命令。

Example:

例子:

vi stylesheet.css

This will open vi editor with the file content.

这将打开带有文件内容的 vi 编辑器。

To start editing, press I

要开始编辑,请按 I

回答by Nick Lesseos

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

你可以使用 git 命令行作为终端我的老兄你只知道命令是 bash 来创建一个文件

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

您可以一次制作任意数量的作品,只需触摸即可

回答by eedrah

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

您可以通过将以下行附加到 .gitconfig 文件来创建别名以在默认编辑器中打开文件:

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

Then, git edit foo.txtwill open the file foo.txtfor editing.

然后,git edit foo.txt将打开文件foo.txt进行编辑。

It's much easier to open .gitconfig with git config --global --editand 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 "..."

打开 .gitconfiggit config --global --edit并粘贴该行要容易得多,而不是弄清楚如何转义所有字符以直接从命令行输入别名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 $VISUALor $EDITORfor 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.editoris 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.
  • fwill execute the function after it is defined.
  • !启动 bash 命令,而不是内部 git 命令
  • f() {...};启动一个函数
  • $(git config core.editor)将从本地配置或全局(如果未设置本地)获取您的编辑器的名称。不幸的是,如果没有设置,它不会查找$VISUAL$EDITOR查找此内容。
  • --将编辑器命令与文件列表分开。这适用于大多数命令行编辑器,因此更安全。如果跳过并且core.editor未设置,则可能执行的是可执行文件而不是被编辑。有了它,命令就会失败。
  • $@将添加在命令行输入的文件。
  • f函数定义后执行。

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:

其他答案对您为什么想要这个表示怀疑。我的用例是我想编辑文件作为我正在构建的其他 git 函数的一部分,我想在用户配置的同一个编辑器中编辑它们。例如,以下是我的别名之一:

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

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

然后,git reedit将打开所有我已经开始修改的文件,git reedit --cached并将打开所有暂存文件。

回答by Ciasto piekarz

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 默认编辑器,但是您可能需要一个小脚本来解析由以下命令生成的文件

git config -l

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

然后变量code.editor保存值/Applications/Sublime_Text.app -n -w

which you can open using os.system()

您可以使用 os.system()

回答by stehuebn

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

我刚刚下载了 Git 2.7.0 并在 .bashrc 中添加了一个别名,用于使用 VS Code 编辑文件:

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

Should also work with other Editors...

还应该与其他编辑一起工作......

回答by dstineback

I used Atom, to open files this works for me

我使用 Atom 打开文件,这对我有用

atom index.html

Hopefully this helps.

希望这会有所帮助。