Create a new file in git bash

159,699

Solution 1

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

> webpage.html

This is actually the same as:

echo "" > webpage.html

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

Solution 2

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

for example

touch filename.extension

Solution 3

echo "" > emptyfile.txt

Share:
159,699
Louis Maddox
Author by

Louis Maddox

• email: [email protected] • github: lmmx • writing: biochemistri.es • reading: naivelocus.com

Updated on October 10, 2021

Comments

  • Louis Maddox
    Louis Maddox over 2 years

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

    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?

    $ 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?

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

    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 git facility on Unix, and only handles the file versioning. git add, git rm, and other git commands 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.

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

    I could have made my new file webpage.html with:

    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 thread for details of setting up a text editor with Git on Windows.

  • Louis Maddox
    Louis Maddox almost 11 years
    So you mean I can create a file with "$ git touch webpage.html" ?
  • Denis Loh
    Denis Loh almost 11 years
    Well, yes and no. git touch is no official command, however you can create an alias for the two bash commands touch $1 && git add $1. touch is a bash command. See git.wiki.kernel.org/index.php/Aliases. You could also have a look on these git extras: github.com/visionmedia/git-extras which contain git touch already
  • Louis Maddox
    Louis Maddox almost 11 years
    Thanks, will look into this
  • Dexter
    Dexter over 9 years
    Does $.gitignore do not work?? $echo "" > .gitignore worked however. I am on Win 7 machine with git version 1.9.4
  • dparpyani
    dparpyani over 9 years
    @Dexter - Did you try > .gitignore? Note the >.
  • Michelangelo
    Michelangelo about 9 years
    Excellent solution. This is really easy, do you maybe know why there is no command like git create index.html?
  • Austin B
    Austin B almost 9 years
    There's a command already for this: touch webpage.html. It will create an empty file. It is available in Git bash as well as MSYS2.