Viewing Changes to a File5:45 with Jay McGavren
You've updated a file, but you've forgotten the specific changes you made. Because Git knows what the previous version looked like, it can show you the differences.
- Git can show you exactly how your files have changed over time.
- We've shown you the
git log -poption, which shows the changes made within each commit.
- But we can also use Git to see changes in our files before we commit them.
- Suppose we added a heading to our site's main page. Open up
medals.html, and at the top add a level 1 heading:
<h1>Welcome to our store!</h1>.
- Also add a descriptive paragraph:
<p>Please choose a section below.</p>
- Once the file is saved, we can use the
git diffcommand to view those changes.
treehouse:~/workspace/medals$ git diff
diff --git a/medals.html b/medals.html
index 5f5fbe7..08982c3 100644
@@ -1,3 +1,6 @@
+<h1>Welcome to our store!</h1>
+<p>Please choose a section below.</p>
- Git will compare the current contents of your files to what's in the staging area, and show you what's changed. If nothing is staged, then it will compare your files with the contents of your previous commit.
- New lines will show with a
+sign in front of them.
- Removed lines will show with a
-sign in front of them.
- And changed lines will show with a
-sign in front of the old version, and a
+sign in front of the new version.
- Now let's stage the file:
git add medals.html.
- Let's try running
git diffagain. This time it exits without showing any output.
- That's because
git diffcompares the contents of your files against the contents of the staging area. If your changes are already staged, then there's no difference to show.
- But there's a command line option that will show you staged changes if you specify it:
git diff --staged. With the
git diffwill compare your staged changes against the previous commit.
Find a Workspace on Treehouse that you've been working in for a while, and turn it into a Git repo. (Or install Git to your computer and convert your favorite project directory to a repo.) You can store HTML files for a website, code files for a program, or even plain text files. If you need a project idea, try filling a directory with text files, where each file is a recipe for a cookbook.
- In the main project directory, initialize a new Git repo.
- Stage some (or all) of the project files, whatever will make a sensible first commit.
- Make your commit. Be sure to use a commit message that completes the sentence "This commit will..."
- Keep staging and committing until all your project files are committed.
- As you keep working on your project files, commit all your changes to the repo.
You need to sign up for Treehouse in order to download course files.Sign up