在 git bash 中创建一个新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18932202/
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 00:13:49  来源:igfitidea点击:

Create a new file in git bash

gitbashshellgithub

提问by Louis Maddox

I've got Git for Windows running, I'm not sure if it's supposed to function as a text editor though?

我已经运行了 Windows 版 Git,但我不确定它是否应该用作文本编辑器?

I think I installed it with the Vim editor, but in a Git Bash shell how do I create a file, such as webpage.html?

我想我是用 Vim 编辑器安装的,但是在 Git Bash shell 中我如何创建一个文件,比如 pages.html?

$ git add webpage.html

comes back as

回来了

fatal: pathspec 'webpage.html' did not match any files

because it tries to track a non-existing file.

因为它试图跟踪一个不存在的文件。

I understand I can create a new file on the github.com interface, that's easy enough, I'm wondering if I can create a new file (like echo in cmd) and whether this file will actually be blank (echo in cmd creates non-blank files) and how I can write into that file from the git bash shell?

我知道我可以在 github.com 界面上创建一个新文件,这很简单,我想知道我是否可以创建一个新文件(例如 cmd 中的 echo)以及该文件是否实际上是空白的(cmd 中的 echo 创建非-blank 文件)以及如何从 git bash shell 写入该文件?

If not, I'm guessing I should just create a new file in windows explorer? Is that the norm...?

如果没有,我猜我应该在 Windows 资源管理器中创建一个新文件?这是常态吗……?

Edit

编辑

Wow, I was new to all this when I asked the above. No, Git Bash isn't a text editor, it's a Windows version of the gitfacility on Unix, and only handles the file versioning. git add, git rm, and other gitcommands just handle how the version control file manager handles the files and folders, and the only things it changes as a result are in a hidden folder named .git. Sorry if this has confused anyone.

哇,当我问上述问题时,我对这一切都很陌生。不,Git Bash 不是文本编辑器,它是gitUnix上该工具的 Windows 版本,并且只处理文件版本控制。git add, git rm, 和其他git命令只处理版本控制文件管理器如何处理文件和文件夹,它因此而改变的唯一内容是在一个名为 的隐藏文件夹中.git。对不起,如果这让任何人感到困惑。

I was confused at the time because, as the name suggests, Git Bash has bash shell commands shipped with it, not just git- e.g. ls(list files), mkdir(make new folder), and -- what I was looking for -- touch(make a new file or update timestamp on existing file), and echo(print text to the command line, or direct that text to a file).

我当时很困惑,因为顾名思义,Git Bash 附带了 bash shell 命令,而不仅仅是git- 例如ls(列出文件),mkdir(创建新文件夹),以及 - 我正在寻找的 - touch(make新文件或更新现有文件的时间戳),以及echo(将文本打印到命令行,或将该文本定向到文件)。

I could have made my new file webpage.htmlwith:

我可以用以下方法制作我的新文件webpage.html

touch webpage.html

Then written to it with:

然后写给它:

echo "<!DOCTYPE html>" > webpage.html

Then appended lines to it with:

然后在其后面附加几行:

echo "<html" >> webpage.html
echo "<head>" >> webpage.html

and so on - but I don't think there's any text editor (according to this list of commands). See this threadfor details of setting up a text editor with Git on Windows.

等等 - 但我认为没有任何文本编辑器(根据这个命令列表)。有关在 Windows 上使用 Git 设置文本编辑器的详细信息,请参阅此主题

回答by dparpyani

If you are using the Git Bashshell, you can use the following trick:

如果您使用的是 Git Bashshell,则可以使用以下技巧:

> webpage.html

This is actually the same as:

这实际上与以下内容相同:

echo "" > webpage.html

Then, you can use git add webpage.htmlto stage the file.

然后,您可以使用git add webpage.html暂存文件。

回答by Denis Loh

Yes, it is. Just create files in the windows explorer and git automatically detects these files as currently untracked. Then add it with the command you already mentioned.

是的。只需在 Windows 资源管理器中创建文件,git 就会自动将这些文件检测为当前未跟踪。然后使用您已经提到的命令添加它。

git adddoes not create any files. See also http://gitref.org/basic/#add

git add不创建任何文件。另见http://gitref.org/basic/#add

Github probably creates the file with touchand adds the file for tracking automatically. You can do this on the bashas well.

Github 可能会创建文件touch并自动添加文件以进行跟踪。您也可以在 上执行此操作bash

回答by MD SHAYON

This is a very simple to create file in git bash at first write touchthen file name with extension

这是一个非常简单的在 git bash 中创建文件的方法,首先写入touch然后是带扩展名的文件名

for example

例如

touch filename.extension