Git

How to Create a New File in Git Bash?

While working on Git, developers need to create files for their development projects which contain source code. Git allows them to create a single as well as multiple files concurrently. More specifically, they can perform different operations on them, such as modifying files, pushing files to the remote repository, and many more.

This study will explain:

How to Create/Make a New Empty File in Git Bash?

To create an empty new file, redirect to the local directory and run the “touch <file-name>” command. The below steps will practically demonstrate the instructions.

Step 1: Move to Desired Repository

First, write out the “cd” command along with the repository path and switch to it:

$ cd "C:\Git\ReposB"

 
Step 2: Create New Empty File

Then, utilize the below-provided command to generate a new file:

$ touch demoFile.txt

 

Step 3: Verify Newly Created File

Ensure whether the new file is created or not by running the “ls” command:

$ ls

 
In the below-provided screenshot, it can be seen that the new “demoFile” has been generated successfully:

How to Create/Make Multiple New Files in Git Bash?

To create multiple files together, the “touch <file1_name> <file2_name> <file3_name>” command can be used.

Step 1: Create Multiple Files

Run the above-mentioned command along with the new files name to generate them:

$ touch F1.txt F2.txt F3.txt

 

Step 2: List Content for Verification

Then, use the following command to verify whether the new files have been created or not:

$ ls

 
It can be observed that the new “F1.txt”, “F2.txt” and “F3.txt” files have been generated:

How to Create and Update a File Simultaneously in Git Bash?

In order to create a new file with some content and or update the already existing file, type out the “echo “<content>” >> <file-name>” command.

Step 1: Create New File With Content

Write out the “echo” command along with the desired content and specify the file name:

$ echo "This is test file" >> testFile.txt

 

Step 2: Verify Existence of Created File

Then, verify the created file with the help of the given-provided command:

$ ls

 

We have efficiently explained the various methods to generate one or multiple files in Git bash.

Conclusion

Git allows users to create single or multiple files at once. The “touch <file-name>” command is used to generate a new empty file or multiple files at once. Furthermore, the “echo “<content>” >> <file-name>” command can also be utilized to create new files with some content or update the content of an already existing file. This write-up explained the methods of creating new files in Git bash.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.