如何在 git-bash 中制作文本文件?

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

How can I make a text file in git-bash?

gittexttext-filesgit-bash

提问by Bruce Irvine

How can I make a text file in Git-bash and then how do I go into that file and add text?

如何在 Git-bash 中创建文本文件,然后如何进入该文件并添加文本?

回答by SVTAnthony

There are many ways to create a file using BASH.

有很多方法可以使用 BASH 创建文件。

using touch touch newFile.txtonly creates.
using echo echo > newFile.txtonly creates.
using cat cat > newFile.txtcreates and can start appending to file.
using vim vim newFile.txtcreates and can start editing the file.

使用触摸touch newFile.txt只创建。
使用 echoecho > newFile.txt只会创建。
使用 catcat > newFile.txt创建并可以开始附加到文件。
使用 vimvim newFile.txt创建并可以开始编辑文件。

Also the .txt extension is only for convenience and categorization. Some editors view the extension to format the data, but for a txtthere's no differnce.

此外,.txt 扩展名只是为了方便和分类。一些编辑器查看扩展以格式化数据,但对于一个txt没有区别。

After creating the file, add it to the git repository.

创建文件后,将其添加到 git 存储库。

git add newFile.txt
git commit -m "added new file"

git add newFile.txt
git commit -m "added new file"

回答by Bruce Irvine

You can use the echo command. Enter the following line into your command prompt to create a new file with the required content:

您可以使用 echo 命令。在命令提示符中输入以下行以创建具有所需内容的新文件:

echo "Put any content here" >> Newfile.txt

If the command line doesn't return any message, this means that you created the file correctly.

如果命令行没有返回任何消息,这意味着您正确创建了文件。

The message in between the double quotes will be the content of the new file.

双引号之间的消息将是新文件的内容。

回答by VonC

In bash alone, you can simple use echo (to initialize it) and vi(to edit it)

仅在 bash 中,您可以简单地使用 echo(对其进行初始化)和vi(对其进行编辑)

echo Text example> aNewFile.txt
vi aNewFile.txt
git add aNewFile.txt
git commit -m "Add aNewFile.txt"

Depending on your environment, you can setup other editors, but in a Windows Git bash, viis the default one.

根据您的环境,您可以设置其他编辑器,但在 Windows Git bash 中,这vi是默认编辑器

回答by CodeWizard

if you are using git bash (or terminal under unix/mac) you simply have to type echo 'text...' >> file_name.

如果您使用的是 git bash(或 unix/mac 下的终端),您只需输入echo 'text...' >> file_name.

if you are no windows machine and you try to do it from cmd its much better to simply create the file with windows explorer.

如果您不是 Windows 机器并且您尝试从 cmd 执行此操作,那么使用 Windows 资源管理器简单地创建文件会更好。

回答by Anga Koko

Use this code to open a new text file

使用此代码打开一个新的文本文件

subl newFile.txt

Here sublrepresents sublime text, my default text editor, and newFile.txtis the new file I want to create and edit. git-bash opens the new file in you default text editor. You can edit and save from there, or just save

这里subl代表 sublime text,我的默认文本编辑器,newFile.txt是我要创建和编辑的新文件。git-bash 在默认文本编辑器中打开新文件。您可以从那里编辑和保存,或者只是保存

To add the new file to stageuse this code

添加新文件以stage使用此代码

git add newFile.txt

ant to committo your reposiory, use this code

蚂蚁到commit您的存储库,使用此代码

git commit