Git Add
Move To Correct Project
If you've been following along, you should have two different directories with Git projects on your computer:
- new-git-project - an empty directory that you converted to a Git repository using
git init
- course-git-blog-project - an existing blog project you retrieved using
git clone
To avoid any confusion with existing commits, we'll be making our commits to the new-git-project Git repository.
On the Terminal, make sure youcd
into thenew-git-project
directory. If you don't have anew-git-project
directory, create it now. Once you're inside the directory, run thegit init
command. If you've already rungit init
before it's ok – runninggit init
multiple times doesn't cause any problems since it just re-initializes the Git directory.
Proper Setup
Time to make sure we both have the same setup:
Task List
I've
cd
ed into thenew-git-project
directory on the TerminalI've run
git init
to create a new Git repository
Your Terminal should look like this:
The Terminal application in the new-git-project directory.
Status Status Status
I've said it a number of times already, but thegit status
command will be_extremely helpful_in this lesson. You should have it as your goal to run thegit status
command both_before_and_after_any other Git command.
Let's run it right now!
The Terminal application showing the output of thegit status
command.
Git Status Output Review
This is the output:
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
💡 Changes in Git v2.14 💡
Remember that in Git version 2.14, the
git status
command changed the wording from "Inital commit" to the much clearer "No commits yet". So the output would be:On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
Notice that last line –nothing to commit (create/copy files and use "git add" to track)
. See how it's recommending thegit add
command? that's super helpful! Thegit status
output will give you advice or hints as to what you should do next.
Let's do what the feedback says and create some files.
Create An HTML File
First, create a file namedindex.html
, and fill it with some starter code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Blog Project</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<link rel="stylesheet" href="css/app.css">
</head>
<body>
<script src="js/app.js"></script>
</body>
</html>
Things to note, the code references a CSS file and a JavaScript file.
Now create the CSS and JavaScript files. You can leave both of these files empty. We'll add content to them in a bit.
Verifying Project Setup
Before we proceed, let's make sure we have the same setup. Check off each of the following as you complete them:
Task List
I have created
index.html
and filled it with the starter code above (and saved the file)I have created a
css
folder and createdapp.css
inside of itI have created a
js
folder and createdapp.js
inside of it
Quick Git Status Check
We just made a number of changes to the repository by adding files and content. It's time to do a quick check-in with Git:
$ git status
Here's what my Terminal displays:
Big Picture Review
That's really cool, isn't it! We haven't done anything specific with Git just yet, but it's watching this directory (since it's a Git project), and it knows that we've created a couple of new files. What's also pretty neat about the output of thegit status
command is that it's telling us that the files are untracked by Git.
Let's do a quick review of what's going on and what we're about to do:
- we have some new files that we want Git to start tracking
- for Git to track a file, it needs to be committed to the repository
- for a file to be committed, it needs to be in the Staging Index
- the
git add
command is used to move files from the Working Directory to the Staging Index - there are currently three, untracked files in the Working Directory
index.html
app.css
in thecss
directoryapp.js
in thejs
directory
So the first step to getting any files committed to the repository is to add them from the Working Directory to the Staging Index. We will be using thegit add
command to move all three of these files to the Staging Index.
The untracked HTML, CSS, and JavaScript files add from the Working Directory to the Staging Index.
Staging Files
Alrighty, it's go time! Run the following command on the Terminal which usesgit add
to addindex.html
to the Staging Index:
$ git add index.html
Note - we are_only_adding theindex.html
file. We'll add the CSS and JavaScript files in just a second.
Running thegit add
command produces no output (as long as there wasn't an error). So how do we have Git tell us what it did and has happened to theindex.html
file that was added? That's whatgit status
does. You're probably sick of me stressing the importance of thegit status
command, but it's an extremely helpful command, especially if you're new to version control and/or the command line.
Let's check out the status of the project:
$ git status
This is the output I get:
Changes To Be Committed
There's now a new section in the output ofgit status
- the "Changes to be committed" area! This new "Changes to be committed" section displays files that are in the Staging Area! Right now it only displays theindex.html
file, so this file is the only item on the Staging Index. To continue this train of thought, if we made a commit right now,only the index.html file would be committed.
TIP: Did you also notice the helpful text that's located just beneath "Changes to be committed"? It says
(use "git rm --cached <file>..." to unstage)
This is a hint of what you should do if you accidentally rangit add
and gave it the wrong file.As a side note,
git rm --cached
is not like the shell'srm
command.git rm --cached
will not destroy any of your work; it just removes it from the Staging Index.Also, this used the word "unstage". The act of moving a file from the Working Directory to the Staging Index is called "staging". If a file has been moved, then it has been "staged". Moving a file from the Staging Index_back_to the Working Directory will unstage the file. If you read documentation that says "stage the following files" that means you should use the
git add
command.
Stage Remaining Files
Theindex.html
file has been staged. Let's stage the other two files. Now we_could_run the following:
$ git add css/app.css js/app.js
...but that's a lot of extra typing. We could use a special command line character to help:
The Period.
The period refers to the current directory and can be used as a shortcut to refer to all files and directories (including all nested files and directories!).
$ git add css/app.css js/app.js
# would become
$ git add .
The only thing to be careful of is that you might accidentally include more files than you meant to. Right now we_want_bothcss/app.css
andjs/app.js
to be staged, so running this command is fine right now. But let's say you added some images to animg
directory but didn't want to stage them just yet. Runninggit add .
_will_stage them. If you do stage files that you didn't mean to, remember thatgit status
will tell you the command to use to "unstage" files.
Stage The Remaining Files
Let's use the shortcut to stage the remaining files:
$ git add .
And then a quickgit status
:
The Terminal application showing the index.html, css/app.css, and js/app.js have been staged.
Git Add Recap
Thegit add
command is used to move files from the Working Directory to the Staging Index.
$ git add <file1> <file2> … <fileN>
This command:
- takes a space-separated list of file names
- alternatively, the period
.
can be used in place of a list of files to tell Git to add the current directory (and all nested files)