Staging Modified Files in Git So They Can be Committed

Once you have created a Git repository, made a commit and then continued to work on your code, the changes you have made since the last commit will be in the “modified” area. This means that Git knows the files have been modified but the changes have not been pushed into the staging area and therefore will not be committed on your next commit.

 

In this tutorial, we will learn how to stage files in Git to ready them for commitment.

 

Show Files Currently in the Git Staging Area

First, let's have a look at files currently in the staging area and therefore waiting to be committed. We can do this with the Git status command.

 

git status
On branch main
Your branch is up to date with 'origin/main'.

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:   download.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)


no changes added to commit (use "git add" and/or "git commit -a")

 

The output in the example above is telling us that the file download.py has been modified but has not been staged for a commit. Running git status can be useful if you forget what files you have modified since your last commit and would like a quick rundown of them.

 

Adding a File to the Staging Area

We can move the Git status of a file from modified into staging for commit by using the Git add command followed by the name of the file. Let's try this with a file, run the status command and see what it shows.

 

git add download.py
git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   download.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 

Cool, we can see that the status of download.py has changed to a ready to be committed state. When we run the next commit it will be included and we can subsequently push that commit to a service like Github.

 

Remove a File from the Staging Area

Sometimes we may accidentally add files to the staging area that should not be committed. We can remove a file from staging by using the Git rm --cached command followed by the name of the file.

 

git rm --cached download.py
rm 'download.py'

 

Now if we run the Git status command again we can see that download.py has been deleted from the staging area and has been moved into the untracked area.

 

git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    download.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	download.py

 

 

Adding Multiple Files to the Staging Area

If you have modified multiple files and need to commit them using Git add for every file is not going to be a great solution. To solve this problem use a . (dot) instead of a file name after the Git add command. This will add every modified file into the staging area.

 

git add .
git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   download.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 

As you can see in the output above download.py was added back to the commit area. I only have one file in my project, but if you have multiple modified files they will all be added.

 

Conclusion

You now know how to use the staging area in Git so you can manage you commits correctly.