Git: Diff & Restore
The git diff command is used to show the differences between various states in your Git repository, such as changes in the working directory, staging area, or between commits. The git restore command is used to discard changes in the working directory or to restore files from a specific commit.
View Differences
Show changes in working directory
git diffDisplays the differences between the working directory and the staging area (i.e., changes that have not yet been staged).
Show changes between staging area and last commit
git diff --cachedDisplays the differences between the staging area and the last commit (i.e., changes that have been staged but not yet committed).
Show changes between two commits
git diff [commit1] [commit2]Displays the differences between two specific commits. Replace [commit1] and [commit2] with the commit hashes or references.
Commit hashes can be found using git log command.
Show changes for a specific file
git diff [file]Displays the differences for a specific file in the working directory compared to the staging area.
Restore Changes
Discard changes in working directory
git restore [file]Discards changes in the specified file in the working directory, reverting it to the last committed state. Use with caution, as this cannot be undone.
Restore file from a specific commit
git restore --source=[commit] [file]Restores the specified file to its state in the given commit. Replace [commit] with the commit hash or reference.