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 --- 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.
- 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.
Another great thing about storing all the prior versions of your files is that 0:00 Git can show you exactly how your files have changed. 0:04 We've shown you the git log -p option, 0:07 which shows the changes made within each commit. 0:10 But we can also use Git to see the changes in our files before we commit them. 0:12 Suppose we added a heading to our site's main page. 0:17 I've opened up medals.html, and here at the top I'll add a level one heading, 0:20 h1, Welcome to our store. 0:25 I'll also add a descriptive paragraph down here. 0:32 Paragraph, Please chose a section below. 0:36 Let me save that and then click back in the console area. 0:44 Now, suppose I got called away for a meeting and 0:47 I had to leave my computer for a while. 0:50 Now I'm back and I'm ready to commit my changes, but 0:52 I can't remember exactly what I've changed. 0:54 We can use the git diff command to view those changes. 0:57 Diff stands for difference, as in, show me the differences in these files. 1:01 Git will compare the current contents of your files to what's in the staging area 1:06 and show you what's changed. 1:10 If nothing gets staged, 1:12 then it will compare your files with the contents of your previous commit. 1:13 New lines show up with a plus sign in front of them. 1:17 Removed lines will show up with a minus sign in front of them. 1:22 And changed lines will show with a minus sign in front of the old version, and 1:25 a plus sign in front of the new version. 1:28 Diffs are one of my favorites features of Git. 1:31 When you're working on a large software project, 1:33 it's easy to lose track of what's changed and where. 1:35 Diffs can immediately find the changes for 1:39 you, even if they're buried in a large file. 1:41 This works regardless of whether you made the change, or another developer did. 1:43 So looking at this diff output, 1:47 I can see that I've added a heading to the main page. 1:49 Now I know what I need to write in my commit message. 1:53 Let's stage the file. 1:56 Git add medals.html. 1:57 Now let's try running git diff again. 2:01 This time it exits without showing any output. 2:04 That's because git diff compares the contents of your files against 2:07 the contents of the staging area. 2:11 If your changes are already staged, then there's no difference to show. 2:12 But there's a command line option that will show you staged changes if you 2:16 specify it, git diff --staged. 2:19 With the staged option, 2:25 get diff will compare your staged changes against the previous commit. 2:26 Now let's commit our changes. 2:30 Git commit -m Add heading. 2:32 With all our changes committed, if we run git diff again, it will show 2:38 no output because there is no difference between our current file contents and 2:43 the staging area. 2:47 There's also no difference between the staging area and the previous commit, 2:48 so git diff --staged doesn't show any output either. 2:54 There's a situation that can come up that can be a little confusing, 2:57 until you understand how the git add command works. 3:01 Suppose we made another change to metals.html. 3:04 We'll add a paragraph to the page, Thank you for shopping with us. 3:08 We save that, click back in our console and add the modified file to staging. 3:16 Git add metals.html. 3:21 Now let's run git status. 3:25 And the file will appear in the changes to be committed section. 3:28 But let's suppose we forgot to add some punctuation on our sentence and 3:33 went back to add it. 3:36 Thank you for shopping with us! 3:37 Save that, and let's click back in the console and run git status again. 3:40 This time, we see the medals.html file as both staged and unstaged. 3:47 This is because git stages the file as it existed at the time we ran git add. 3:52 You can see this if we run git diff --staged. 3:58 What's staged is the version of the paragraph with no punctuation. 4:04 If we run git diff without the double dash staged option, 4:08 it will compare our modified file with the staged file and 4:12 show us the difference, that is, the exclamation point. 4:15 We can update the staged version by running git add on the file again, 4:18 git add medals.html. 4:22 If we run git status again, we'll see that the changes not staged for 4:25 commit section is gone. 4:30 There's only a changes to be committed section, and 4:32 that section includes medals.html. 4:35 If we run git diff, there will be no output, 4:39 showing there's no difference between our modified file and the staging area. 4:42 And if we run git diff --staged, 4:47 it will show only the version of our paragraph that has punctuation. 4:50 Now we can safely commit our staged file, git commit -m Add thanks. 4:55 So remember, if you stage a file, 5:04 git staged is a snapshot of the file at the moment you staged it. 5:06 If you modify the file after that, 5:10 you'll need to run git add on it again to stage the latest changes. 5:12 In this stage, we showed you how to initialize a Git repository and 5:17 commit files to it. 5:21 Practicing your new skills is important to remembering them, so 5:22 be sure to check the teacher's notes on this video for some practice ideas. 5:25 We promised you that Git could also keep you from losing work. 5:31 In the next stage, you'll see commands that will restore accidentally deleted 5:34 files, bring back file contents from old commits, and more. 5:37 Ready? 5:42 We'll see you in the next stage. 5:43
You need to sign up for Treehouse in order to download course files.Sign up