How do I track untracked files in git?

You can start tracking untracked files by using git add. Further, you can git commit to storing the changes in the form of commits. You can push them to the remote repository to permanently keep track even if the local repository is lost.

Let us see through a hands-on example.

Consider this initial setup where there is a branch with the name main. It has eight commits, as shown by git log –oneline command.

~/repoone$ git log --oneline
d0978f9 (HEAD -> main, origin/main, origin/HEAD) Sixth line in newfile
9870df7 Update newfile
27658cf Fourth line added in newfile
bead66e Another change to new file
3f54558 This is new file
4632c0d Adding empty directory
908d6ac Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

We will create a new file fileseven to create the scenario of untracked files.

~/repoone$ touch fileseven

You can see that fileseven is considered an untracked file now, and git is giving the solution too. If we do not do anything now, if this file is removed, there is no way to get it back or know we removed it. 

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

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

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

We can use git add to add it to tracked files.

~/repoone$ git add fileseven

git status would show that it is tracked now.

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   fileseven

Now, removing the file and doing git status will show that it is deleted.

~/repoone$ rm fileseven

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   fileseven

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    fileseven

You can use git restore and quickly get the file back. This is possible as you made it tracked file in git. Without git add, you cannot reverse the changes we might make.

~/repoone$ git restore fileseven
ubuntu@ip-172-31-0-61:~/repoone$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   fileseven

Note that we can get back to the state before we did git add. Any intermediate changes that you do before git add will not get tracked. Also, it is always a good idea to store it as a local commit. This will keep the changes permanently available in a commit. 

Further, it is a good idea to push the commits regularly to the online repository to keep the storage permanent in the online repository.