If you add files to your local machine then try to pull a remote copy of a repository without adding those files to the repository, you’ll encounter the “nothing added to commit but untracked files present” Git error.
This guide discusses what this error means. We’ll walk through two potential solutions to this issue and their benefits and drawbacks.
nothing added to commit but untracked files present
Git is a distributed version control system. This means you can make a local copy of a repository and make your own changes to that repository. These changes are not reflected in the main project until you commit them.
When you pull code from a remote server, make sure that all the files on your local machine are part of the Git staging area, or part of a commit. This is because Git needs to know what files are part of a repository.
Executing a git pull command on a repository where you have not added every file to the staging area or a commit will result in the following error:
nothing added to commit but untracked files present
Untracked Files: A Primer
Untracked files are files inside a project that have been configured with Git, but have not become part of the Git repository.
Files only become part of a Git repository if you add them using the “git add” command. This lets you create and modify files in your local machine that do not have to become part of a line of development in a repository.
Untracked files should either be added to a project, or ignored using a rule in a .gitignore file.
The Solution
There are two potential solutions to this error.
The first solution is to add all the untracked files into a Git repository. Do this using the git add command:
git add file_to_add.md
This will move the file into the staging area. When you add a file to the staging area, it becomes tracked. This is because all files in the staging area are added to the next commit you create, unless they are removed. Git needs to track these files to know to add them to a commit.
The second option is to ignore the files you have added to your repository. This option is best taken if you do not want the files you have created to become part of the Git repository. This is common for configuration files that store API keys or local dependency storage folders.
You can ignore files by adding an entry to the .gitignore file in your project:
file_to_add.md directory/
You can place a gitignore file in any directory in your project. It’s best to keep your .gitignore in the root folder of your repository so that it is easy to access. All the rules in a .gitignore file will recursively apply to the files and folders within the folder in which the .gitignore file is stored.
Now that we’ve solved the error, we can pull the most recent version of a Git repository using the git pull command:
git pull
This command should succeed because we are now tracking or ignoring all the files that are in the project folder.
Conclusion
The “nothing added to commit but untracked files present
” error is raised when you create new files in your local working copy of a repository and forget to add them to the staging area before you pull a new version of the repository.
To fix this error, either add the files causing the error to the staging area or ignore them using the .gitignore file. Now you have the knowledge you need to fix this common Git error like a professional.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.