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
Let's add files to your new Git repo!
-
git
commands won't work outside a Git repo. - Whenever you exit a repo directory, be sure to change back into the directory with
cd
before issuing any Git commands.
We've initialized a Git repository in our project's directory. But our project files haven't been added to the repository yet. In fact, Git has no idea they exist! In this video, we're going to fix that, by staging and then committing these files.
Our project directory is known in Git terminology as the working directory, because it's the directory where we actually edit and do other work on our files. Right now our working directory contains two files, medals.html
and bronze.html
, that haven't been added to Git.
File statuses
There are three states every file goes through in a Git repository.
- When you make changes to a file in the working directory, it's "modified".
- You don't necessarily want to include all of these modified files in your next commit, so you need to specify which ones you will include. You do this by adding files to the index, more commonly known as the "staging area" or "cache". The staging area is where you place the files you're going to commit. Files you've added to the index are referred to as "staged" files.
- When you've staged all the files you want, you make a commit, and that's when the files are actually added to your Git repository.
- Then, when you next make a change to any of those files, they're treated as "modified" again. You can stage and commit the files again to save a new version of them. And the cycle repeats.
About git status
- We can find out what state our project files are in, using the
git status
command. - Its output includes a list of "untracked files". These are files that Git isn't "tracking" yet - it's not keeping track of changes to them so we're not saving new versions of them.
- Its output also includes helpful messages suggesting commands to run next.
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# medals.html
# bronze.html
nothing added to commit but untracked files present (use "git add" to track)
Staging files
- Let's try the suggested command:
git add medals.html
- There's no output from the command, which is actually a good thing. Many Git commands produce output only when there's an error. Empty output means "everything went fine".
- The
git add
command adds a file to the staging area, the place we compose our commit. - Now, let's try running
git status
again, to see if that's changed. - The output is now in two sections.
- There's a new "Changes to be committed" section, which lists files in the staging area. That list consists of one file right now,
medals.html
. - The "Untracked files" section looks much the same as before, except that now it only includes
bronze.html
.
- There's a new "Changes to be committed" section, which lists files in the staging area. That list consists of one file right now,
Committing files
- We use the
git commit
command to commit our staged changes. - You need to provide a message to go with every commit, a brief note explaining what the commit does.
- We do this with the
-m
option.-m
should be followed by a string in quotation marks. Since this commit addsmedals.html
, we'll use a message of"Add main site page"
.
git commit -m "Add main site page"
- As a rule of thumb, a commit message should complete the phrase "This commit will:"
- "This commit will: Add main site page"
- "This commit will: Remove sale description"
- "This commit will: Add new products", etc.
Git configuration
- Run the
git commit ...
command, and we'll see an error.- In addition to a commit message, Git needs to know your name and e-mail address so it can attach them to the commit.
- This is another of Git's collaboration features. It allows other people working on the project to contact you if they need to ask about the commit.
- The name and e-mail address are permanently stored as part of Git's configuration. The
git config
command allows you to add and edit values in that configuration. - Let's run the commands it suggests:
git config --global user.email "jay.mcgavren@teamtreehouse.com"
git config --global user.name "Jay McGavren"
Completing the commit
- Now, with a user name and email stored, I should be able to make a commit successfully.
- Most shells keep a history of commands you've entered. You can hit the up arrow key to bring up previous commands, so you don't have to type them again.
- This isn't a feature of Git, it's a feature of the shell itself.
- Hit the up arrow until the
git commit
command is active, and then hit Enter to re-run it. - This time, we'll see a message confirming the commit.
Using an editor for commit messages
- If you leave off the
-m
command line option when runninggit commit
, Git will launch a text editor so you can enter a commit message. - Git launches the text editor with a pre-formatted file, with an empty line at the top for you to type your commit message, and some comments below that explaining what to do.
- We've set up Workspaces to use a text editor called
nano
for editing Git messages. - With
nano
, you just type your message normally: "Add main site page" - Next, we need to save the file and exit the editor.
-
nano
displays commands you can run across the bottom of the screen. - The carat (
^
) character stands for the Ctrl key. So Ctrl-G will "Get Help", Ctrl-O will write out the file, etc. - We want to press Ctrl-o to write out the file.
- The file name has already been set up for us, so we should just press Enter to accept the existing file name.
- Then we can press Ctrl-x to exit the editor.
- When the editor exits, Git will read your commit message from the saved file, and use that to complete the commit.
- This gives us the same result as if we had used the
-m
option togit commit
.
On other systems, Git uses an editor named vi
by default.
Viewing Git logs
- Once a commit is complete, it's a permanent part of the repository's history.
- You can review that history with the
git log
subcommand.- For each commit,
git log
shows the: - Author name and e-mail
- Date and time of the commit
- The commit message
- If you want, you can add the
-p
option togit log
:git log -p
. Thengit log
will show the lines that were added in each file. - Anytime a git command shows output that's too long for the screen, it will show that output using a pager program. You can use the up and down arrow keys to scroll through the output, and press the
q
key when you're ready to quit back to the shell prompt.
- For each commit,
More options
Those are the basics of the git add
and git commit
commands. You can do almost anything with what we've already shown you, but there are additional options with git add
and git commit
that can make it easier to work with large numbers of files. See the teacher's notes if you'd like to learn more.
- If you pass a directory name to
git add
, it will stage all the files that directory contains:git add mydir
- You can specify the current directory with
.
. Sogit add .
will stage all files in the current directory, and all its subdirectories. - You can stage changes to all tracked files (files you've added in a previous commit) with the
-a
option togit commit
. So if you've changed two files that are already tracked, you can stage and commit the changes in a single command withgit commit -a -m "My message"
. (This can also be shortened togit commit -am "My message"
.)- To prevent accidentally committing the wrong files, the
-a
option does not add "untracked" files; files that have never been committed before. You'll need to stage those withgit add
first. -
Note: The code challenges for this course want you to stage and commit files using separate commands, so you won't be able to use
-a
in code challenges.
- To prevent accidentally committing the wrong files, the
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