Objective: In this assignment, you will add new files to your existing project, learn what a .gitignore file is, and use it to prevent Git from uploading sensitive files or junk files to your public GitHub profile.
Prerequisites: Completion of Part 1 (You should have your git-assignment folder on your Desktop, linked to GitHub).
First, we need to open Git Bash and navigate back into your project folder.
Step 1: Open Git Bash from your Windows Start Menu. Step 2: Navigate to your project folder on the Desktop by typing:
cd ~/Desktop/git-assignmentStep 3: Make sure everything is up to date by checking your status:
git status(It should say "nothing to commit, working tree clean".)
Let's add some actual content to your repository.
Step 1: Let's create a couple of text files for your project. Type the following commands:
echo "Git is awesome!" > notes.txt
echo "1. Learn Git 2. Drink Coffee" > todo.txtStep 2: Run git status. You will see notes.txt and todo.txt listed in red as "Untracked files". This is normal! Git sees them but isn't saving them yet. Do not commit them just yet.
Sometimes, you create files in your project folder that you never want to send to GitHub. Examples include:
- Passwords or secret API keys
- Personal notes
- Messy system log files created by your computer
Step 1: Let's pretend you have a file with a secret password, and a messy log file. Create them by typing:
echo "MY_SECRET_PASSWORD=12345" > passwords.env
echo "Error at line 42" > debug.logStep 2: Run git status again.
Notice that passwords.env and debug.log are sitting there in red. If you were to run git add . right now, your secret password would be staged to go to the internet! We need to stop Git from seeing these files.
A .gitignore file is a special, hidden text file. Any file or folder name you type inside it will be completely invisible to Git.
Step 1: Create the .gitignore file and open it in Windows Notepad directly from Git Bash:
notepad .gitignore(Note: Make sure there is a "dot" at the very beginning of the word gitignore. A Notepad window will pop up asking if you want to create the file. Click Yes.)
Step 2: In the Notepad window, type the exact names of the files you want Git to ignore. Type the following on separate lines:
passwords.env
*.log
(Why *.log? The asterisk * is a wildcard. It tells Git to ignore ANY file that ends in .log, no matter what its name is!)
Step 3: Save the file in Notepad (Ctrl + S) and close the Notepad window.
Let's see if it worked.
Step 1: Go back to your Git Bash window and run your favorite command:
git statusStep 2: Look closely at the output.
- You will see
.gitignore,notes.txt, andtodo.txtin red. - The magic:
passwords.envanddebug.loghave completely disappeared from the list! Git is now ignoring them. Your secrets are safe.
Now let's formally save our new files and our .gitignore file, and send them to GitHub.
Step 1: Stage all the safe files:
git add .Step 2: Commit them with a descriptive message:
git commit -m "Added project notes and configured gitignore"Step 3: Push the changes to GitHub:
git pushStep 1: Go to your web browser, open your GitHub repository, and refresh the page.
Step 2: Verify that notes.txt, todo.txt, and .gitignore are there.
Step 3: Verify that passwords.env and debug.log are NOT there.
📝 How to Submit Your Assignment:
- Take a screenshot of your GitHub repository page showing the new files.
- Submit the screenshot along with the link to your repository.
- Answer this short question in your submission: Why is it important to use a
.gitignorefile before runninggit add .?