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 know how to commit new files, now let's look at committing changes to existing files. (Hint: the process is nearly identical.)
We've committed our first version of medals.html
to our repository. But medals.html
doesn't include a link to bronze.html
right now, so users of the site will have no way to find that page. We'll need to edit medals.html
to add that link, and commit those changes as well.
Add the following code to medals.html
:
<div>
<a href="bronze.html">Bronze medals</a>
</div>
- Be sure to save your changes after editing the file.
- Don't worry if you don't understand all that. If you want, you can learn more about HTML in this course.
Now, we need to commit our changes to the file.
- We've modified the
medals.html
file. Let's see what its status is, with thegit status
command:git status
- There's a new section, "Changes not staged for commit". This is where changes to tracked files will appear.
- Our
medals.html
file is in that section, listed as "modified".
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: medals.html
#
no changes added to commit (use "git add" and/or "git commit -a")
- We can see a helpful message in the output telling us what to do next: 'use "git add
..." to update what will be committed'. - So, let's run the command
git add medals.html
. -
git add
doesn't just add untracked files to the staging area. It also stages the changes within tracked files. - If we run
git status
again, we'll see the modifiedmedals.html
is now listed under the "Changes to be committed" section. We've staged the changes to be committed.
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: medals.html
#
Untracked files:
(use "git add <file>..." to include in what will be committed)
bronze.html
- But we can also see that
bronze.html
, the file our change refers to, is down in the "Untracked files" section. - So, as the helpful message in the "Untracked files" section says, we'll run
git add
to addbronze.html
to the staging area:git add bronze.html
- Let's run
git status
again:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: medals.html
# new file: bronze.html
- Now both files are in the "Changes to be committed" section,
medals.html
as "modified" andbronze.html
as a "new file". - Now we can commit them both at once:
git commit -m "Add bronze medals"
- Now that the commit is complete, we can run
git status
... - And it will show us that we have no modified files and no untracked files.
- It says the working directory is "clean", meaning it doesn't have any changes that aren't also in the Git repository. Everything is committed!
$ git status
# On branch master
nothing to commit, working directory clean
Only Staged Files Are Committed
Sometimes you'll have multiple modified files, but you won't want to commit all of them.
Suppose I had added copyright info to the medals.html
file, and marketing text to the bronze.html
file:
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: bronze.html
modified: medals.html
no changes added to commit (use "git add" and/or "git commit -a")
The two changes are totally unrelated. It would probably be best to handle them as two separate commits. But if all modified files were always committed, that wouldn't be possible. I'd have to undo changes to one file before I could make a commit.
But that's why Git has a staging area. It allows you to select individual modified files to be part of a commit, while leaving other modified files to be part of a separate commit.
That means that if I want, I can stage and commit only the bronze.html
file:
$ git add bronze.html
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: bronze.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: medals.html
$ git commit -m "Add marketing text"
[master 6e93661] Add marketing text
1 file changed, 2 insertions(+)
Because the medals.html
file isn't staged, it will be left out of the commit and left in a "modified" state:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: medals.html
no changes added to commit (use "git add" and/or "git commit -a")
Then I can stage medals.html
and make it part of a separate commit:
$ git add medals.html
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: medals.html
$ git commit -m "Add copyright info"
[master c9ac1a0] Add copyright info
1 file changed, 1 insertion(+)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
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