Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 -p
option, 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 diff
command to view those changes.
treehouse:~/workspace/medals$ git diff
diff --git a/medals.html b/medals.html
index 5f5fbe7..08982c3 100644
--- a/medals.html
+++ b/medals.html
@@ -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.
The --staged
option
- Now let's stage the file:
git add medals.html
. - Let's try running
git diff
again. This time it exits without showing any output. - That's because
git diff
compares 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--staged
option,git diff
will compare your staged changes against the previous commit.
Practice
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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up