In this post, we will see how we can move the files from the staging area back to the unstaging area. Also, we will see how we can unmodify the changes we made using git commands.
Suppose I made some changes into example.txt and then executed “git status” command then it gives me following expected output:
Above example.txt file is in modified area. Now I’ll add that file in staging area.
Here as we can see example.txt is in the staging area since it is highlighted in green color. So it is ready to be committed but for some reasons, we don’t want to commit this file now and want to unstage this file, that we can use the following command:
git restore --staged filename.extension
So here as we can see that example.txt is highlighted in red color and showing changes not staged for commit means it is in unstaged area. Now further if you see we can restore the changes we have made in example.txt using the following command:
git restore filename.extension
Here we can see that working tree is clean now. So this is how we can unstage and unmodify the files in git.
Getting the previous commit file
Suppose I made some changes to any file but after some time I realize that I don’t want these changes, I want the content of the previous file. So this thing can be done by git command.
Above I have appended the last line in the example.txt file and then executed the git status command and as usual, it gives me modified example.txt. To discard these changes I can use the following command:
git checkout -- filename.extension
This command works only on the files which are not in staging area. If you try to checkout any file after adding it in staging area it won’t show any effect.
So above command gives me previously committed file contents. So this command is very useful whenever we want to get the previously committed content.
Now suppose there are multiple files that you want in their previous commits. So there is one command to get the previous commit for those many files. Command is
git checkout -f