Lesson 2 - Tracking Changes
In the previous module you committed a single file. In this module you’ll change that file, see what changed, and commit the changes. You’ll also learn a shortcut for committing files Git already knows about.
The sandbox starts each module with the repository as you left it at the end of the previous module.
Changing a File
Add a paragraph to the page. The sed command inserts a new line before the closing </body> tag:
sed -i 's| </body>| <p>This is the first paragraph.</p>\n </body>|' index.html
cat index.htmlNow check the status:
git statusThe status shows that you modified index.html. Git noticed the change because it tracks this file.
Viewing Diffs
The git diff command shows the difference between what you committed and what you changed:
git diffLines that start with a + sign are lines you added. Lines that start with a - sign are lines you removed. The surrounding lines give you context.
Check the diff before every commit so you know what you’re about to record.
Staging the Change
The status told you what to do. You have to stage the file before you can commit it. It’s always this process:
- Make a change.
- Stage the file you changed.
- Make a commit.
Stage the file:
git add index.htmlBefore you commit, add a second paragraph so you can see how Git handles staged changes and changes you haven’t staged:
sed -i 's| </body>| <p>This is the second paragraph.</p>\n </body>|' index.html
git statusThe file shows up twice, once in each section of the output. When you stage a file, Git stages the content as it exists at that moment. If you change the file again, you have to stage it again to include the new changes.
Follow the directions and stage the file again:
git add index.html
git statusNow the file appears only in the first section. Commit the changes:
git commit -m "Add two paragraphs to home page"The output reports 2 insertions, one for each paragraph.
A Quicker Commit Process
You can skip the staging step for files Git already tracks. Add a third paragraph:
sed -i 's| </body>| <p>This is the third paragraph.</p>\n </body>|' index.html
git statusThe status shows that the only change is to a file Git already knows about. The -a flag on git commit stages every tracked file that changed, so you can stage and commit in one step:
git commit -a -m "Add third paragraph to home page"The -a flag stages all tracked files that changed. That’s dangerous if you changed files you didn’t mean to change. Always review the status before you use it.
Your Turn
Change the title of the page from AwesomeCo to AwesomeCo Home. Use git diff to review the change, then commit it with a single command:
sed -i 's|<title>AwesomeCo</title>|<title>AwesomeCo Home</title>|' index.html
git diff
git commit -a -m "Update page title"
git statusWhat You Learned
git statusshows modified filesgit diffshows which lines changed- Staging records a file’s content at that moment, so stage again after further changes
git commit -astages and commits every tracked file in one step
In the next module you’ll review the history of the repository and travel back in time.