Introduction: The Day I Lost 3 Days of Work
In my early days, I used to copy-paste my project folder to create a backup. One day, I accidentally overwrote a file with an old version. I lost 3 days of work. I wanted to cry. That's when a senior developer introduced me to Git. It changed my life. In this guide, I'm going to teach you the only 5 Git commands you need to get started.
What is Git and GitHub?
Git is a version control system. It's a tool that tracks changes in your files. Think of it as a time machine for your code. GitHub is a cloud platform where you can host your Git repositories. It's like a social network for developers, but it's also where your code lives.
Core Concept: The Three Stages
- Working Directory: The files you are currently editing on your computer.
- Staging Area: Where you prepare changes to be committed.
- Repository: The permanent history of all your commits.
Step-by-Step Execution
- Install Git: Download from git-scm.com and install it.
- Initialize a Repository: Open your terminal, navigate to your project folder, and run `git init`.
- Stage Changes: Run `git add .` to stage all files.
- Commit: Run `git commit -m "Initial commit"`. This saves a snapshot.
- Connect to GitHub: Create a new repo on GitHub. Copy the remote URL. Run `git remote add origin
`. - Push: Run `git push -u origin main` to upload your code to GitHub.
What About Branches?
Branches are a way to work on new features without breaking the main code. You can create a branch using `git checkout -b new-feature`. Once you're done, you can merge it back into the main branch.
Common Mistakes Beginners Make
- Forgetting to commit: If you don't commit, your changes aren't saved in the history.
- Committing everything blindly: Use a `.gitignore` file to exclude sensitive files (like `.env`) or large folders (like `node_modules`).
- Not pulling before pushing: If you're working with a team, always `git pull` before you `git push`.
Practical Exercise (Don't Skip This)
Create a new folder, add a simple text file, and initialize a Git repository. Commit the file. Then, create a new branch, make a change, and commit it. Finally, merge it back to the main branch. If you get stuck, email me at digisoneglobal@gmail.com.
Frequently Asked Questions
Q: Is GitHub the only platform?
A: No. GitLab and Bitbucket are popular alternatives. However, GitHub is the industry standard for open-source.
Q: Do I need to know the command line?
A: It's highly recommended. You can use a GUI tool like GitHub Desktop, but learning the CLI will make you a better developer.
Tools & Resources Mentioned
- Git - Download here.
- GitHub - The platform.
- Git Cheat Sheet - Essential commands.
Conclusion & Next Steps
Git is a skill that pays for itself tenfold. Once you get comfortable with it, you'll never go back to manual backups. Next, we'll dive into how to collaborate with others on GitHub.