10 Minutes
Git
has a number of capabilities to simplify your life, help you understand the status of your directory and improve your ability to communicate about your changes.
When using git
, sometimes you want to know more about the changes you are committing ... this is where git diff
comes in.
When you have changed multiple files, it is sometimes desireable to add them to the staging area in batches. git add
also has this capability
For this next exercise, we will change a single file and use the git diff
tool to examine those specific changes in detail.
- Use your text editor to open the
beowulf.txt
file in the Codeless project. - Change the letter
B
in the first lineBeowulf (modern English translation) ...
to a lowercaseb
. - Execute the following command:
git diff
This will show you, in detail, the differences between the last version of the file that was committed via git
and any changes you have since created. For details on how this is broken out, see the Deep Dive section.
With your own projects, for 95% of what you do, git status
and git diff
should be sufficient to regularly track your progress and confirm the changes you have made. You will use these commands, along with git add
, git commit
, git push
more than any others.
NOTE: We will cover several more advanced concepts in later discussions.
It can be very helpful to see what's different between the file you are working on and the last-committed version. To do this, use the git diff
command.
git diff
represents changes in a manner referred to generally as "diff format". This format most basically represents all changes as the addition or removal of lines from files.
Note: the examples below are in black/white, but in most command-line environments (bash
, for example) they will also be color-coded to help make them easier to read.
If I add a line to a file, I'll see something like this:
$ git diff
diff --git a/rpncalculator/parser.py b/rpncalculator/parser.py
index 90cd3cd..e22a262 100644
--- a/rpncalculator/parser.py
+++ b/rpncalculator/parser.py
@@ -39,6 +39,7 @@ class Parser(object):
"""scan input stream and return token generator"""
if isinstance(stream, str):
stream = StringIO(stream)
+ print(str(stream))
for line in stream:
tokens, remainder = _scanner.scan(line)
for t in tokens:
The first part of the output shows me which file is being displayed. The output from git diff
can contain many files; you can also specify individual files or groups of files by providing the file name(s) to git diff with git diff <file1> <file2> ...
. Even with a single file specified, the output will always show you which file(s) are being compared with each change.
The line that starts with +
indicates that line is new compared to what's currently committed. Also note that the tool shows me a few lines before and after the change to help me see the context of the change.
Also you can see which line number(s) are involved in the change. The @@
line tells you that line 39 is the affected line. There are two line numbers there because changes to multiple places in a file can mess up your idea of line numbers. Just remember that the first number is the line in the original file and the second number is the line in the modified file.
Similarly, if I remove a line:
$ git diff
diff --git a/rpncalculator/parser.py b/rpncalculator/parser.py
index e22a262..90cd3cd 100644
--- a/rpncalculator/parser.py
+++ b/rpncalculator/parser.py
@@ -39,7 +39,6 @@ class Parser(object):
"""scan input stream and return token generator"""
if isinstance(stream, str):
stream = StringIO(stream)
- print(str(stream))
for line in stream:
tokens, remainder = _scanner.scan(line)
for t in tokens:
Here, the -
shows me the line that was removed.
If I change a line, it will show as a removed line and an added line:
$ git diff
diff --git a/rpncalculator/parser.py b/rpncalculator/parser.py
index e22a262..b186e9a 100644
--- a/rpncalculator/parser.py
+++ b/rpncalculator/parser.py
@@ -39,7 +39,7 @@ class Parser(object):
"""scan input stream and return token generator"""
if isinstance(stream, str):
stream = StringIO(stream)
- print(str(stream))
+ log.debug(str(stream))
for line in stream:
tokens, remainder = _scanner.scan(line)
for t in tokens:
Here you can see that I changed the print() call to a log.debug() call.
A file with multiple changes would look like this:
$ git diff
diff --git a/rpncalculator/parser.py b/rpncalculator/parser.py
index e22a262..7e8bf4a 100644
--- a/rpncalculator/parser.py
+++ b/rpncalculator/parser.py
@@ -39,7 +39,7 @@ class Parser(object):
"""scan input stream and return token generator"""
if isinstance(stream, str):
stream = StringIO(stream)
- print str(stream)
+ log.debug(str(stream))
for line in stream:
tokens, remainder = _scanner.scan(line)
for t in tokens:
@@ -50,6 +50,7 @@ class Parser(object):
if not engine:
engine = Engine()
result = None
+ log.debug(str(stream))
for token in self.scan(stream):
if isinstance(token, float) or isinstance(token, int):
result = engine.push(token)
Here I can see that I changed line 39 from a print()
to log.debug()
and that I also added a new log.debug()
call at line 50.
Many GUI-based tools will show you more information, such as highlighting the individual characters in a line that were changed. Since this varies from tool to tool, it won't be covered in detail here.
If you need to add more than one file to the staging area, simply separate the filenames with a space:
$ git add <file1> <file2> ...
Previous | Up | Next |
---|---|---|
Cloning a Repository | Using Git | Branching and Merging |