Staging in Git



Once you are done with the changes on your project code base, you need to commmit your files to the GIT repository. But before you commit, you need to stage your files in a Staging area. You can think of it like a waiting area or self-review area.

Git Image

Viewing the changes


Before staging any file for committing them, we should first know which all files have changes and which files out of them do you want to stage. To see the list of files having changes, enter the following command in your terminal.

git status                        
                    

This command will list down all the files which are having changes compared to the last commit. One such sample response is shown below:

On branch master
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:   file.txt
        modified:   second.txt

no changes added to commit (use "git add" and/or "git commit -a")
PS F:\GITHUB\git3\git-tutorial-scratch> git status
On branch master
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:   file.txt
        modified:   second.txt

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

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

The list of files will be categorized under the sub headers of tracked and untracked files. Untracked file refers to the files which are created newly and are to be committed for the first time.



Staging the changes


To stage your changes to git, you can use the below command:

git add file-name                       
                    
or (both are synonyms of each other)
git stage file-name                       
                    

Replace the file name with the relative path of the file to be staged. If you have multiple files you would be required to manually type the relative paths of the files.

To save time, you can just copy the file names (including the relative file paths) displayed to you while you used the "git status" command and paste it while executing the "git stage" commands.



Staging all the files


If you have a lot of files and you would like to stage all the files having the changes, then you can use a short cut as shown below:

git add .        
    
or
git add --all
    
or
git add -A
    

Once you are done staging, you should recheck to confirm the files are really staged or not. For that you can again try entering "git status" command to see the list of staged files. You will observe that the staged file names will be shown in Green color.



Unstaging a file


You may come across scenarios where you staged a file by mistake. You can unstage such files using this command:

git restore --staged file-name                        
                    


Unstaging all files


If you wish to unstage all the files which are in staging area, you can use the shortcut command

git restore --staged .                        
                    


That's all for this tutorial. If you liked reading this tutorial, do share it with your friends.

What's Next? Head on the Git Commit tutorial

Recommendations
Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount