Add modifications in the Git local repositories is common for developers while working on extensive development projects. When new changes are added, these are placed in the Git working area when new changes are added. The users need to track them to the staging index. Additionally, developers can view the added changes in a particular file.
This post will provide the procedure for viewing changes to a file before committing.
How to See Changes to a File Before Commit?
To display the changes to a file before committing, try the following steps:
-
- Redirect to the required repository.
- Generate and add a new file to the tracking index.
- Update the repository by committing changes.
- Open the existing file and modify it.
- Push newly added changes to the staging area.
- Check the repository’s current status.
- Execute the “git diff –cached” command.
Step 1: Switch to Git Repository
First, move to the Git required repository by running the “cd” command:
Step 2: Generate New File
Next, execute the “touch” command to create a new file in the current working repository:
Step 3: Track Changes
Then, track a newly created file from the working area to the staging index by utilizing the “git add” command:
Step 4: Commit Tracking Area Data
After that, update the Git repository by pushing the staged changes into it:
Step 5: Update Existing File
Now, use the “echo” command to modify the existing file with some text:
Step 6: Git Add Changes
Next, add all changes to the tracking index by utilizing the “git add” command:
Step 7: View Git Status
After that, run the provided command to check the status of the current working repository:
As you can see, the newly modified file “file1.txt” exists in the tracking index and need to be committed:
Step 8: View Changes Before Commit
Finally, execute the “git diff” command to view the added changes in the particular file before committing:
Here, the “–cached” flag is used to show the staged changes. As you can see in the below-given output, the highlighted text is most recently added in the staged “file1.txt” file:
That’s all! We have provided the procedure for showing changes to a file before committing.
Conclusion
To view the changes to a file before committing, first, redirect to the required repository. Then, generate and add a new file into the staging area. Next, update the repository by committing. After that, open the existing file and add some text. Push newly added changes to the staging index and check the repository’s current status. Lastly, execute the “git diff –cached” command. This post described the method of viewing changes to a file before committing.