Lesson 4 - Adding More Files
So far you’ve worked with a single file. Real projects have more. In this module you’ll add a style sheet, handle files in different states, and stage several files at once. The sandbox starts with the repository from the previous modules.
Adding a Stylesheet
The web page needs a style sheet. Create the file style.css:
cat > style.css <<'CSS'
* {
box-sizing: border-box;
}
body { background-color: #aaa; }
CSS
cat style.cssThen link the style sheet from index.html by inserting a <link> tag before the closing </head> tag:
sed -i 's| </head>| <link rel="stylesheet" href="style.css">\n </head>|' index.html
cat index.htmlFiles in Two States
Check the status of the repository:
git statusYou have files in two states. You modified index.html by adding a line to it. Git doesn’t track style.css because you haven’t added it yet.
Look at the diff:
git diffThe diff shows the change to index.html but says nothing about the style sheet. Git only compares tracked files, and it only tracks files you’ve added.
You could commit the change to index.html with git commit -a, but that won’t pick up the new style sheet. The -a flag only stages files that Git already tracks. Stage the style sheet explicitly:
git add style.css
git statusNow style.css appears as a new file in the staging area, but index.html still isn’t staged. Stage it too:
git add index.html
git statusBoth files are ready. Commit them:
git commit -m "Add stylesheet"The output reports that 2 files changed and that Git created style.css.
Staging Several Files at Once
You’ve staged files one at a time, but git add accepts a list of files or a folder. Create a couple of pages and a folder of scripts:
echo "<h1>About</h1>" > about.html
echo "<h1>Services</h1>" > services.html
mkdir js
echo "console.log('hello');" > js/app.js
git statusStage all three in one command. When you give git add a folder, it stages everything inside it:
git add about.html services.html js/
git statusCommit them:
git commit -m "Add about and services pages with scripts"You can also use git add . to stage everything in the current folder. Review the status first so you don’t stage files by accident.
The Workflow
The workflow is always the same with Git:
- Make changes to your code.
- Use
git statusto see what you need to do. - Use
git diffto review what you changed. - Use
git addto stage the files you changed. - Use
git commit -m "your message"to take a snapshot of your code.
Commits are cheap. They take up little space, so commit as often as you like. Commit whenever you get something working.
Using Git on an Established Project
You can start using Git on a project whenever you like, even on a project that’s almost done. Follow these steps:
- Navigate to the folder that contains your project.
- Use
git initto initialize the repository. - Use
git add .to stage every file. - Commit the files with
git commit -m "Initial import".
From then on, follow the workflow.
Your Turn
Create a folder called images with two files in it. Stage the folder with a single git add command and commit it:
mkdir images
echo "logo" > images/logo.txt
echo "banner" > images/banner.txt
git add images/
git commit -m "Add images"
git statusWhat You Learned
- A repository can hold new, modified, and staged files at the same time
git diffonly shows changes to tracked filesgit commit -anever picks up new filesgit addaccepts several files or a whole folder
In the next module you’ll share your repository with another location.