Before you create a commit, you have to add the files you have changed to that commit. When you run the
git status command before adding files to a commit, you’ll see the
changes not staged for commit message in the output of the command.
In this guide, we’re going to discuss what this message means and why it is important. We’ll walk through an example of how you can stage the files you need to add to a commit.
changes not staged for commit
Files in a Git repository can either be ignored, in the staging area, or part of a commit.
Ignored files are not included in the record of a Git repository. Files in the staging area are those that are going to be added to the next commit.
The staging area is important because it lets you choose which files should and should not be added to a commit. You can add or remove files from the staging area at any time before you create a commit.
This means the staging area is somewhat of a triage space. If you realize an additional file needs to be added into a commit, you can add it to staging. Then, once you are sure you have added all the changes to the staging area, you can create a commit.
An Example Scenario
To receive this message, we must first change a file in a Git repository. Suppose we have a Git repository with a blank file called README.md. We’re going to change its contents to show the following:
# Example Repo
We have changed a file in our repository. Next, we’re going to run the git status command to view a summary of all the files that have changed:
Let’s see what this command displays:
On branch master
Your branch is up to date with ‘origin/master’.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: README.md
The Git command line tells us we are viewing the master branch and our current branch is up to date with our remote branch. We have changed one file: README.md. This file has not yet been added to the staging area or a commit. Our working directory is modified.
To make this message go away, we have to add the README.md file to the staging area. We can do this using the git add command:
git add README.md
This command lets us selectively choose files to add into a commit. Next, we can create a commit with the files we have changed that are currently in the staging area. Let’s run git commit to create a commit:
git commit -m “docs: Update README.md”
This will create a record of the current state of the repository with all of the changes we’ve added to the staging area. If you are working with a repository with a remote version, you may want to push your commit to the repository after making it:
Our change has now been made to both the local and remote versions of our repository.
Let’s take a look at the git status on branch again again:
On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
The command tells us there are no changes that have not been added to a commit or the staging area. This means we have successfully changed our repository. There is now one additional commit in our repository which contains the change we made to README.md.
The “changes not staged for commit” message shows when you run the “git status” command and have a file that has been changed but has not yet been added to the staging area.
This is not an error message, rather a notification that you have changed files that are not in the staging area or a commit. You can make the message go away by adding your files to a commit and committing them to a repository.
Now you have the knowledge you need to fix this Git error like a professional developer!