Different Ways to Commit Untracked Files in Git

Different Ways to Commit Untracked Files in Git

John Wachira Sep-08, 2022 Git Git Commit

This article discusses the different methods we can use to commit untracked files in Git. If you introduce new files to your project in Git, these files will fall under the Untracked Files category.

Regarding the Git version control system, these files do not exist in your project. So, how do you commit such files?

Different Ways to Commit Untracked Files in Git

Let’s begin with the conventional way of doing it.

To commit untracked files, we will first instruct Git to start tracking the files. This is done by adding the files to the staging area using the git add command.

Let’s say we have an untracked styles.css file. To track the file, we will run:

$ git add styles.css

Pretty simple, right? What if we have a hundred untracked files?

Adding the files one by one will take up a lot of time. Instead, you can run the command with the ., as illustrated below.

$ git add .

This will add all the untracked files to the index. The index is also called the staging area.

We can commit the files using the git commit command, as shown below.

$ git commit -m "Adding new Files"

We use the -m flag to include a commit message. If you run the git commit command by itself, Git will open a text editor for you to provide a commit message.

Why not make work easier with the -m flag?

How long does that take? Thirty seconds? A minute?

Luckily, a command that can make it even quicker takes us to our second method.

In the previous section, we used two commands, i.e., git add and git commit. As illustrated below, we can combine the two to make our work simpler and cleaner.

$ git commit -a -m "Adding new Files"

This command will commit all files, whether tracked or untracked. How long did that take? Much faster, right?

You can use the git add and git commit commands separately to commit untracked files or combine the two by adding the -a flag to the git commit command. Whichever you choose, you will arrive at the same destination.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.


Related Article - Git Commit

  • Get Current Commit in Git
  • Git List Commits
  • Git Add and Git Commit in One Command
  • git add, git commit, and git push in One Command
  • Commit Current Changes to a Different Branch in Git
  • Exit the Commit Message Editor