
The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.
List commits
Let's say there is a file named foo.txt in the currently selected branch of the cloned repository. The git log command can be used to view the history of commits.
~]# git log foo.txt
commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date: Mon May 29 20:26:09 2020 -0500
Initial commit
Compare local file to commit
Let's say you update foo.txt, replacing Hello with World, without committing the change. The git status command should show that foo.txt has been modified.
~]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: foo.txt
The git diff foo.txt command should now show there are differences between foo.txt on your PC as compared to the latest commit of foo.txt in the currently selected branch of the cloned repository. If no output is produced, this means there are no differences between foo.txt on your PC as compared to the latest commit of foo.txt in the currently selected branch of the cloned repository.
~]# git diff foo.txt
diff --git a/foo.txt b/foo.txt
index bcd9498..8932028 100755
--- a/foo.txt
+++ b/foo.txt
- Hello
+ World
Compare commits
Let's say you commit foo.txt.
git commit --message 'replaced Hello with World' foo.txt
The git log command should now contain the new commit.
~]# git log foo.txt
commit dkci85474fjfdkd9393934k49f9fk002kd01
Author: John Doe <john.doe@example.com>
Date: Tue May 30 18:23:36 2020 -0500
replaced Hello with World
commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date: Mon May 29 20:26:09 2020 -0500
Initial commit
By default, each commit will contain difference, as Git only allows you to make a new commit when some change is made to a file. You can use the git diff command to output the differences between commits.
]$ git diff mks910122020slsmm3lsosos020399489sl:example.txt fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl:example.txt
diff --git a/mks910122020slsmm3lsosos020399489sl:example:example.txt b/fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl:example.txt
index cf96eb3..5cebaa2 100644
--- a/mks910122020slsmm3lsosos020399489sl:example:foo.txt
+++ b/fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl:foo.txt
@@ -1,6 +1,6 @@
- World
+ Hello
Differences between branches
In this example, the files that have differences between the "main" and "feature/foo" branch will be listed.
~]$ git diff --name-only master...feature/foo
example.txt
The git diff origin master command can be used to list the differences between every file in the currently selected branch of the cloned repository and the origin Git repository.
~]# git diff origin master
diff --git a/foo.txt b/foo.txt
index bcd9498..8932028 100755
--- a/foo.txt
+++ b/foo.txt
- World
+ Hello
Did you find this article helpful?
If so, consider buying me a coffee over at