Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to use Git on Command Line
  7. Commit a file
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Commit a file

We are going to add a new file and have it registered under the repository we just created.

Create a file named sample.txt in that directory with the following text content.

Anyone can learn Git with this tutorial and Backlog

We can use the git status command to confirm the status of the "tutorial" directory.

$ git status
On branch main
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	sample.txt
nothing added to commit but untracked files present (use "git add" to track)

We can tell from the response that sample.txt is currently not being tracked. You must first add sample.txt to the index to track it.

Use the git add command followed by the file you want to add to the index. If you want to add multiple files, separate them with a space. Then verify that sample.txt has been successfully added to the index.

You can specify . instead of individual file names to add all files in the current directory to the index

$ git add sample.txt
$ git status
On branch main
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   sample.txt

Now that sample.txt has been added to the index, we can proceed with committing the file. Use the git commit command and again check the status.

$ git commit -m "first commit"
[main (root-commit) 13fc237] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 sample.txt


$ git status
On branch main
nothing to commit, working tree clean

The command’s response tells us that there are no more new changes to be committed.

We can see the newly added commit in the repository’s history log with the git log command.

$ git log
  commit ac56e474afbbe1eab9ebce5b3ab48ac4c73ad60e
  Author: username
  Date:   Thu Jul 12 18:00:21 2022 +0900

      first commit

Next, you’re ready to share the repository with your team.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life