Git and GitHub: The Only Guide You'll Ever Need
Table of contents
If you're a developer or aspiring to become one, you've likely heard of Git and GitHub. They are fundamental tools for version control and collaboration on coding projects, especially in open-source communities. However, getting started with Git and GitHub can seem daunting, especially if you're new to these tools.
In this article, I'll break down everything you need to know, from creating a GitHub repository to cloning projects and contributing to open-source. By the end of this guide, you'll not only understand the commands and processes but also feel confident enough to use Git and GitHub in your projects.
Table of Contents |
1. What are Git and GitHub? |
2. Setting Up Git Locally |
3. Creating a GitHub Repository and Uploading Code via GitHub Website |
4. Creating a Repository Locally and Pushing Code from Terminal/Command Prompt |
5. Cloning Someone Else’s Project and Making Contributions |
6. Understanding Key Terms |
7. Conclusion |
1. What are Git and GitHub?
Before diving into the commands, let's clear up what Git and GitHub are.
Git is a version control system, which means it tracks changes in your files, particularly your code, over time. It lets you revert back to previous versions, collaborate with others, and work on different parts of a project simultaneously using branches.
GitHub is a cloud-based platform that hosts Git repositories. It allows you to share your Git-tracked code with others, making it easier to collaborate on projects. GitHub provides an interface for managing Git repositories and also offers features like issue tracking, pull requests, and more.
In short, Git is the tool, and GitHub is the platform where your Git repositories are stored.
2. Setting Up Git Locally
To use Git, you need to install it on your computer. Follow these steps:
Download and install Git:
For Windows, download Git from Git’s official website.
For Mac users, Git should come pre-installed, but if not, use Homebrew:
brew install git
.On Linux, you can install Git using your package manager:
sudo apt install git
(Ubuntu/Debian).
Configure Git with your name and email: Once installed, you need to set up your name and email, which will be linked to your commits (code changes).
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
This step ensures that Git knows who is making changes to the project.
3. Creating a GitHub Repository and Uploading Code via GitHub Website
Creating a Repository on GitHub
To create a repository on GitHub:
Go to GitHub and sign in (or sign up if you don’t have an account).
On the top right, click the + icon and select New Repository.
Fill in the repository name (e.g.,
my-first-repo
).Choose the visibility (public or private) and click Create Repository.
Uploading Code via GitHub Website
If you want to upload code manually through the GitHub website:
Open your repository.
Click on the Add File button and select Upload Files.
Drag and drop your files or select them manually.
Add a commit message describing the changes (e.g., "Initial commit").
Click Commit Changes to upload your code.
4. Creating a Repository Locally and Pushing Code from Terminal/Command Prompt
You can also create a repository locally on your machine and push it to GitHub.
Step 1: Initialize a Local Repository
Create a directory for your project and navigate to it using your terminal or command prompt:
mkdir my-project cd my-project
Initialize the directory as a Git repository:
git init
This creates a
.git
folder, which will track your code changes.
Step 2: Add Files and Make a Commit
Add your project files to the repository:
git add .
The
.
adds all files in the current directory. You can also add files individually (e.g.,git add index.html
).Commit your changes with a descriptive message:
git commit -m "Initial commit"
Step 3: Push the Code to GitHub
Go to your GitHub account, create a new repository as described in the previous section, but don’t add any files or README.
Copy the repository URL (e.g.,
https://github.com/your-username/my-project.git
).Link your local repository to GitHub:
git remote add origin https://github.com/your-username/my-project.git
Push your code to GitHub:
git push -u origin main
If your default branch is named
master
, replacemain
withmaster
.
5. Cloning Someone Else’s Project and Making Contributions
GitHub enables collaboration by allowing users to clone repositories, make changes, and contribute back to the original project.
Cloning a Project
Find the repository you want to clone.
Click the Code button, and copy the HTTPS URL (e.g.,
https://github.com/username/repo-name.git
).Clone the repository:
git clone https://github.com/username/repo-name.git
This downloads the project files to your local machine.
Making Changes
Navigate into the project folder:
cd repo-name
Make your changes using your preferred code editor.
Stage and commit your changes:
git add . git commit -m "Descriptive commit message"
Pushing Changes
Push your changes back to your own GitHub fork:
git push origin main
To contribute your changes back to the original project, open a Pull Request (PR). This allows the original repository owner to review and merge your changes if they find them valuable.
6. Understanding Key Terms
Here’s a quick glossary of essential Git and GitHub terms:
Repository (repo): A directory that contains all the project files and tracks their changes.
Commit: A snapshot of your changes with a message explaining what was modified.
Branch: A parallel version of your project. You can have multiple branches to work on different features without affecting the main project.
Clone: A copy of someone else’s Git repository on your local machine.
Fork: A personal copy of someone else’s GitHub repository, allowing you to experiment without affecting the original.
Pull Request: A request to merge changes from one repository or branch into another. Often used in open-source projects.
Remote: A version of your project that is hosted on a server (e.g., GitHub).
Merge: Combining changes from one branch into another.
7. Conclusion
By now, you should have a solid understanding of how to use Git and GitHub. Whether you're starting your projects or contributing to open-source, these tools will make it easier to manage code, collaborate with others, and track changes.
Remember, the more you practice, the more intuitive these commands and processes will become. So, clone a project, make some changes, and push your code with confidence!
Happy gitting!
Subscribe to my newsletter
Read articles from Huzaifa Saran directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Huzaifa Saran
Huzaifa Saran
Django full stack developer and Emerging software engineer