Git - Stage files with Git Add

This article covers how to use the command 'Git Add'

So, you've made some changes; Some of them you wish to push (upload) to the remote repository, and some of them you want to keep on your machine only. For git to distinguish which files will be committed, first you need to stage them.

Initially all your changes will be untracked. To stage them, you can use the git add command:

git add <file-path>

You can do this for multiple files at once, simply by supplying multiple filenames:

git add my-post.md my-image.jpg my-page.html

You can also track all untracked items, simply by adding a . as the file name:

git add .

Let's test this. Currently I have made changes to this post in my local repository, so if I were to call git status, we should see it as an untracked file:

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        content/posts/20210302-using-git-add.md

nothing added to commit but untracked files present (use "git add" to track)

Great! Now let's assume I've finished, and add this file:

git add content/posts/20210302-using-git-add.md

Notice how the full path needs to be given, relative to the root of the repository. We should see if this has worked, by running git status again:

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   content/posts/20210302-using-git-add.md

Perfect! The file changes will now be included once I commit. But what if I make more changes? Will they also be included? Let's save this file, and re-run git status a third time

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   content/posts/20210302-using-git-add.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   content/posts/20210302-using-git-add.md

It's important to note that staged changes apply to the code at the time of staging. Any additional changes will result in a staged version of your file, and an untracked version that has been modified after git add was last run.