So you want to master Git and GitHub?
Been meaning to write about this for a while now. Honestly, I struggled with Git and GitHub for months, so here's what I learned, the hard way. When I first tried using Git, I made this stupid mistake of not understanding branches, and I messed up my project big time. Let's make sure you don't do the same.
Getting Started: Git Installation
If you're like me, you've probably wondered, 'Why on earth do I need Git in the first place?' Trust me, dude, once you get the hang of it, you'll see it's a lifesaver for version control and collaboration.
First, install Git. If you're on Windows, download it from git-scm.com. For Mac and Linux, you can just use Homebrew or your package manager:
$ brew install gitPro tip from someone who's been there: don't skip the configuration step. Set your name and email:
$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"Your First Repository
Let's create your first repo. Imagine you're starting a new project. Move to your project directory:
$ cd my-awesome-projectAnd initialize a new Git repository:
$ git initNow, if you're wondering, 'How do I keep track of my changes?', just use:
$ git statusThis command is your best friend. It'll tell you what's up with your files every time.
Using GitHub: Your Code's New Home
Okay, you've got your local Git thing going. Time to push it to GitHub so the world (or just your teammates) can see it. Spoiler: it took me 3 hours to debug what was a typo in my remote URL. Don't be like me. 😅
Create a new repository on GitHub. Then, link your local repo to it:
$ git remote add origin git@github.com:yourusername/my-awesome-project.gitPushing your Code
Add your changes to staging, commit them, then push:
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin mainThis snippet saved my project, hope it helps you too!
Collaborating with Others
Here's where GitHub shines. When building Project X, I had to collaborate with three other developers. We used branches to work on different features simultaneously:
$ git checkout -b new-featureMerge conflicts can be a pain, but they're manageable if you communicate and pull changes regularly:
$ git pull origin mainBtw, I wrote about handling merge conflicts in more detail here, check it out!
Final Thoughts
I'm not an expert, but this is what worked for me. There are better ways, but this is what I use. Feel free to correct me in the comments if there's a better approach! Try this out and let me know how it goes. I'll update this post if I find something better.
If you enjoyed this, you might like my post on branching strategies. Drop a comment if you get stuck anywhere!