Git for non-programmers 2

A couple of definitions

When you add changes with the git add command you are adding to the index. What you add from is called the working tree, this is the current state of the files in your file system.

The commit log

Now, if you want to check what changes you made, a good place to start is the log. git log will print out a list of commits with the commit message you entered at each stage.

Example

$ git log
commit f7c36c5e87b103e7431cb5b886ce6e16706ee0fb (HEAD -> master)
Author: Adam Bark
Date:   Mon Jul 15 16:40:35 2019 -0500

    Add command line parsing

    Add a description with the argparse library

commit f0192b6f277557df49d066c571760b1730e9261a
Author: Adam Bark
Date:   Mon Jul 15 13:41:50 2019 -0500

    Make executable

    Turn the script into an executable programming by adding execute
    permissions and adding the appropriate shebang.

commit 73ae5322e3073a20758f7d7c98b8b3955ec3a4ea
Author: Adam Bark
Date:   Sun Jul 14 17:07:47 2019 -0500

    Initial commit

as you can see I have added a couple of extra commits to make the examples more useful.

Find out what’s changed

git diff

This command without any other qualifiers will show you the difference between the index and the working tree i.e. the difference between what’s in your files and what you have ‘added’.

Example of changes between the working tree and the index

$ git diff
diff --git a/universe.py b/universe.py
index 6ae3560..8537b02 100755
--- a/universe.py
+++ b/universe.py
@@ -5,4 +5,4 @@ import argparse

 parser = argparse.ArgumentParser(description='Say hello to the universe')
 parser.parse_args()
-print('Hello, Universe!')
+print('Hello, World!')

Example of changes since a specific commit

$ git diff f0192b6f277557df49d066c571760b1730e9261a
diff --git a/universe.py b/universe.py
index 1461d6b..8537b02 100755
--- a/universe.py
+++ b/universe.py
@@ -1,3 +1,8 @@
 #!/usr/bin/env python3

-print('Hello, Universe!')
+import argparse
+
+
+parser = argparse.ArgumentParser(description='Say hello to the universe')
+parser.parse_args()
+print('Hello, World!')

Here you can see that I specified commit f0192b6f277557df49d066c571760b1730e9261a. Git names commits with a hash, hence the odd string but you may have noticed that the commit hash is included in the log so they’re not too difficult to use. There is also a handy shortcut to the last commit, also called HEAD; @ refers to HEAD and you can also specify an offset like so: @{1} is the commit before HEAD and @{n} is the nth commit before head.

Example of changes between a range of commits

$ git diff @{2} @{1}
diff --git a/universe.py b/universe.py
old mode 100644
new mode 100755
index 8e37e82..1461d6b
--- a/universe.py
+++ b/universe.py
@@ -1 +1,3 @@
+#!/usr/bin/env python3
+
 print('Hello, Universe!')

This shows the change between the initial commit (@{2}) and the commit immediately after it.

Conclusion

That wraps up some simple commands to see what you changed, next time we’ll cover how to get back to a previous version.

git 

See also