On Git, users can generate and add multiple files to the projects. They can modify files whenever they need. Additionally, developers are allowed to view the list of all staged files. For this purpose, the most commonly used command is “git status .” which displays the current state of the directory and the staged content.
Another Git command, the “git diff” is also used to display all added changes among the Git working directory and its HEAD. In other words, it combines the “git log” and “git status ” commands.
This post will discuss the method of listing staged files in Git.
How to List Git Staged Files?
If developers want to view the list of all staged files, they can perform this operation with the help of the Git different commands, such as “git status .”, “git diff –name-only –cached” and many more.
Let’s step up and check the practical demonstration of the above-stated commands!
Step 1: Redirect to Git Repository
At first, move to the required Git local repository through the provided command:
Step 2: Generate and Update File
To make and update a new file, execute the “echo” command:
Here, we have created a new python file in the repository:
Step 3: Track Changes
Next, use the following command to push the working directory changes into the staging index:
Step 4: Create and Modify File Simultaneously
In order to generate and modify the file immediately, use the below-stated command:
Step 5: Stage Changes
Next, execute the “git add” command to track the unstaged changes from the working area:
Step 6: View the Repository Status
After that, check the current status of the Git working directory through the “git status .” command:
According to the below-provided output, the current repository contains the “file1.py” and “file2.txt” staged files:
Step 7: View the Staged Files Using git diff Command
Another way to list the staged files is by executing the “git diff” command:
Here:
- “–name-only” flag is used only to show the name of particular files.
- “–cached” flag is the alternative of the “–staged” option.
As you can see, the above-stated command gives the list of all staged files placed anywhere in the Git directory along with their path:
That’s all! We have explained the procedure of listing the Git staged files.
Conclusion
To view the list of the Git staged files, first, move to the Git required repository. Then, generate and track new files into the staging index. Check the current status of the repository. After that, execute the “git diff –name-only –cached” command to display the list of all staged files. This post demonstrated the process of listing Git staged files.