We will show you how to view the changes or differences between your work copy, which is the files that you’re working on in the main files, and the repository. In the second part, we will demonstrate how to compare between the staging area and the repository.
Check Differences between Working Copy and the Repository
Once we saved a file, this file on a working copy is different from the one in our repository. But how exactly do we view these changes? Because sometimes we made some changes to a file and we want to view the one in the repository and this happens a lot whenever you’re working with code because you want to know exactly the changes you are doing and how this is going to affect the main project.
So the first thing that we can do is to check the
git status and you find one file is modified, meaning it’s different from the one in the main project or the main repository. If you type
git diff and hit enter, it is going to show you the differences.
$ git diff diff --git a/test1.txt b/test1.txt index e1dd8e3..448ad04 100644 --- a/test1.txt +++ b/test1.txt @@ -1,2 +1,2 @@ -This is my first Git repository. -New added text. \ No newline at end of file +This was my first Git repository. +This line is updated. \ No newline at end of file
The red text in the git bash shows what is in the repository and the green one indicates how the text is modified.
If you have more files updated after last commit,
git will list the file differences one by one.
Git diff external tools
If you haven’t got used to git default
diff tool, you could configure the
difftool to the one you prefer.
We will show you how to configure
kdiff3 as the
merge tool of
.gitconfig file directory
In Windows OS, open the
.gitconfig file in the directory
C:\Users\username and append the text to the file,
[diff] tool = kdiff3 [difftool "kdiff3"] path = "C:/Program Files/KDiff3/kdiff3.exe" trustExitCode = false [difftool] prompt = false [merge] tool = kdiff3 [mergetool "kdiff3"] path = "C:/Program Files/KDiff3/kdiff3.exe" trustExitCode = false [mergetool] keepBackup = false
path is the installed path of
kdiff3, and it could be different on your side.
difftool via git bash
You could configure the
mergetool by using the command in git bash, too.
git config --global --add merge.tool kdiff3 git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe" git config --global --add mergetool.kdiff3.trustExitCode false git config --global --add diff.guitool kdiff3 git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe" git config --global --add difftool.kdiff3.trustExitCode false
It has done basically the same thing as you modify the
.gitconfig file content.
Check Differences between Staging Area and the Repository
You could use
git difftool command to review the file difference between that in your working copy and that in the repository.
git diff shows the difference between the working copy and the repository, and after the file is already added to the staging area,
git diff will not show any difference of this file because it does not compare the staging area against the repository, so that’s why it looks like everything is up-to-date, even though this file is still different than the one in your repository.
The right command to compare the staging are and the repository is to add option
$ git diff --staged diff --git a/test1.txt b/test1.txt index e1dd8e3..448ad04 100644 --- a/test1.txt +++ b/test1.txt @@ -1,2 +1,2 @@ -This is my first Git repository. -New added text. \ No newline at end of file +This was my first Git repository. +This line is updated. \ No newline at end of file