Git & GitHub 101: Saving Your Work Like a Pro
Have you ever been afraid to change your code because you might break it? Version control is the answer. Think of it like a "save game" system for your code. Git is the most popular version control system, and GitHub is a website to store your Git projects online.
What is Version Control?
Version control tracks changes to your files over time. This means you can:
- See who changed what and when.
- Go back to a previous version if you make a mistake.
- Collaborate with others on the same project without overwriting each other's work.
Git Basics: Your Local Repository
Git works on your local machine. Here are the most common commands:
git init: Initializes a new Git repository in your project folder.git status: Shows the status of your changes.git add <file>: Adds a file to the "staging area," which is a list of changes you want to save. Usegit add .to add all files.git commit -m "Your message": Saves the staged changes with a descriptive message.git log: Shows a history of all your commits.
GitHub: Your Remote Repository
GitHub is a platform for hosting your Git repositories in the cloud. This allows you to back up your code and collaborate with others.
- Create a GitHub Account: Go to https://github.com and sign up.
- Create a New Repository: Click the "New" button on your GitHub dashboard.
- Push Your Code: Follow the instructions on GitHub to connect your local Git repository to the remote GitHub repository and "push" your commits.
AI for Commit Messages
Writing good commit messages is important. If you're stuck, your AI can help!
"Write a short, imperative git commit message for the following changes: [paste output of
git diff]"
Your Turn: Push to GitHub
- Go to the
my-first-projectdirectory you created in the last lesson. - Run
git init. - Create a
README.mdfile and write a short description of your project. - Run
git statusto see your new file. - Run
git add README.md. - Run
git commit -m "Add initial README file". - Create a new repository on GitHub and push your first commit!
This is a fundamental workflow for all modern developers.