Git & GitHub 101: Getting Started
Git is one of the most essential tools in a developer's toolkit, and GitHub is its powerful counterpart that allows us to collaborate and store code online. If you're new to version control, this guide will cover what Git and GitHub are, how to install Git on Windows, and some fundamental commands to get you started.
What is Git?
Git is a distributed version control system that allows you to track changes in your code over time. Imagine you’re working on a big project with several files, and you want a way to:
- Track changes to each file.
- Go back to previous versions if needed.
- Collaborate with others without overwriting each other's work. That’s exactly what Git does! It keeps a history of changes, so you can undo mistakes, experiment with new features, and even collaborate with a team—all while keeping your code organized.
What is GitHub?
GitHub is a web-based platform that lets you store and share your code repositories online. Think of GitHub as a remote storage space for your Git repositories, with additional tools for collaboration.
- Share your code with others.
- Collaborate on projects, even with people around the world.
- Use it as a portfolio to showcase your work to potential employers. GitHub is built on Git, so it uses the same commands and concepts.
Installing Git on Windows
To start using Git, you'll first need to install it on your computer. Here’s how to set it up on Windows. Step 1: Download Git
- Go to the official Git website: git-scm.com.
- Click Download for Windows. The download should start automatically. Step 2: Install Git
- Open the installer once it’s downloaded.
- Go through the installation wizard, keeping the default settings. When prompted, select GitBash as the default command-line interface for Git.
- Complete the installation. Once it’s installed, you can check if Git is working by opening Git Bash and typing:
1git --version
If you see the Git version number, you’re ready to go!
Basic Git Commands
Now that Git is installed, let’s walk through some common commands you’ll use. Open Git Bash and follow along.
Setting Up Git for the First Time
Before you start, set your name and email, so Git knows who you are. This info will show up in your commit history.
1git config --global user.name "Your Name" 2git config --global user.email "you@example.com"
Creating a New Repository
A Git repository (or “repo”) is where your project lives. To start tracking a project with Git, navigate to its folder in Git Bash and initialize it.
1cd path/to/your/project 2git init
This creates a hidden .git
folder in your project directory, where Git will keep track of your files and changes.
Tracking Changes
Once your project is under Git’s control, you can start tracking changes. Add files to staging:
1git add filename
o add all files in the directory:
1git add .
Commit changes to the repository:
1git commit -m "A short message describing your change"
The commit command takes a “snapshot” of your project at this point in time. The message should be concise but descriptive of the changes.
Viewing Commit History
To see a list of commits in your project, use:
1git log
This command will show each commit, who made it, and a unique ID for each change.
Getting Started with GitHub
With Git installed and some changes committed locally, let’s connect your project to GitHub so you can back it up and collaborate with others.
Step 1: Create a GitHub Account
If you haven’t already, go to GitHub and sign up for an account. Once you’re signed in, create a new repository:
- Click the + icon in the top right corner.
- Select New repository.
- Name your repository and click Create repository.
Step 2: Linking Your Local Repo to GitHub
Once you have your GitHub repository, connect your local repository to it by adding it as a “remote.”
- Copy the HTTPS or SSH URL from your new GitHub repository page.
- In Git Bash, set it as the remote origin:
1git remote add origin https://github.com/yourusername/your-repo-name.git
Step 3: Push Your Code to GitHub
Now, let’s upload our local commits to GitHub so that others can see your work. Run the following command to push your changes:
1git push -u origin main
This sends your local code to the main branch on GitHub, making it available online.
Summary of Basic Commands
Here’s a quick cheat sheet for the Git commands we covered:
- Initialize a repository:
git init
- Add files:
git add filename or git add .
- Commit changes:
git commit -m "message"
- Set a remote:
git remote add origin URL
- Push to GitHub:
git push -u origin main
Conclusion
Git and GitHub can seem overwhelming at first, but with these basics, you’re ready to start tracking your projects, experimenting with new features, and sharing code with others. As you continue learning, explore additional commands like git branch
, git merge
, and git pull
to deepen your understanding.
Now, you’re set to use Git and GitHub in your workflow. Practice these commands, experiment, and soon you’ll be working with Git like a pro!