Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Adding New Files | Introduction to Git
Git Essentials
course content

Conteúdo do Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

book
Adding New Files

Creating a Text File

Let's first create a text file in our project directory, so that our working tree will not be empty. We can do this using the echo command:

'Learning Git is cool!' is the text content we want to write to our file, the single quotes are just used for enclosing the text and won’t be written to the file. By default, the echo command outputs the text to the terminal, however we use the output redirection operator > to redirect the output to the test.txt file.

Let’s now run the git status command:

Now, there is a file in the working tree, however, it is currently untracked by Git. In order for it to be tracked, we should add this file to the staging area.

Adding to the Staging Area

The staging area, also known as the "index", is a file which serves as an intermediate step between your working directory and the Git repository itself. It contains information regarding the changes and files which will be included in the next commit.

A commit is a fundamental operation that records changes made to files in your Git repository. Essentially, when you commit, you are creating a snapshot of your project at a particular point in time and store it.

The command to add a file to the staging area is as follows:

<file> should be replaced with the name of the file or its path relative to your project's root directory if the file is located in a specific directory within your project. The command looks as follows for us:

Let’s now add our test.txt file to the staging area and check the status of our working tree and the staging area:

Note

In fact, the git status command displays the state of both the working directory and the staging area.

As you can see, test.txt has been added to the staging area, representing a single change ready to be committed – the addition of a new file. Here is an illustration to make things clear:

How to add a file named data.csv to the staging area?

How to add a file named data.csv to the staging area?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 7
We're sorry to hear that something went wrong. What happened?
some-alt