Your First Steps in Git and GitHub A Step-by-Step Guide for Newbies
Ashwin Kumar
Posted on September 23, 2024
➤ Welcome to the Git and GitHub Guide!
So, you’re here, which means you’re starting to learn about Git and GitHub. You’ve come to the right place! In this post, I’ll explain everything in a simple way that you won’t forget. This guide is for beginners, but I’ll share some resources at the end for those looking for advanced commands.
I assume you already know how to create a repository on GitHub and that you have Git installed on your system. If you’d like me to create a separate post on how to do this, feel free to comment below!
➤ What is Git, Anyway?
- Version Control System: Git is a tool that helps you track changes in your code.
- Local Repository: You keep a copy of your project on your computer.
- Collaboration: It makes it easy to work with others on the same project.
- History: Git keeps a history of all your changes, so you can go back if needed.
➤ What is GitHub, Anyway?
- Cloud Service: GitHub is a platform to host your Git repositories online.
- Collaboration Hub: It allows you to share your projects and collaborate with others.
- Public and Private Repos: You can make your projects public or keep them private.
- Community: GitHub has a huge community where you can discover projects and contribute.
Now that you know what Git and GitHub are, let’s jump into some simple GitHub kickstarters. In the industry, you’ll mostly work on GitHub, although there are other cloud-based services like GitBucket.
➤ How to Push Code to GitHub
Let’s say you’ve created a project and want to push it to your remote repository. Here’s how you can do it step by step:
Create a Repository on GitHub: Go to GitHub and create a new repository. Copy the URL of this repository for later use.
Open Your Terminal: Navigate to your project folder where your code is located.
Initialize Git:
git init
-
What it does: This command initializes a new Git repository in your project folder. It creates a hidden
.git
directory, where Git stores all the necessary metadata and version history for your project.
- Stage Your Changes:
git add .
-
What it does: This stages all the changes in your project. It tells Git that you want to include these changes in your next commit. The
.
signifies that you want to add all files.
- Commit Your Changes:
git commit -m "Initial commit"
-
What it does: This commits the staged changes to your local repository. The
-m
flag allows you to add a message describing the changes. Here, we’re labeling it as "Initial commit" to indicate the first version of the project.
- Link Your Local Repo to GitHub:
git remote add origin <GitHub_repo_URL>
-
What it does: This links your local repository to the remote repository you created on GitHub.
origin
is the default name for this link, and you replace<GitHub_repo_URL>
with the URL you copied earlier.
- Push Your Code to GitHub:
git push -u origin main
-
What it does: This pushes your committed changes to the remote repository on GitHub. The
-u
flag sets the upstream branch, allowing you to push to the same branch in the future without specifying it again.origin
is the remote name, andmain
is the branch name (usemaster
if that’s your default branch).
And that’s it! Your repo is now online.
➤ Making Future Commits
If you want to make changes later, just follow these commands:
- Stage Your New Changes:
git add .
- What it does: This stages any new or modified files for commit.
- Commit Your Changes:
git commit -m "Your message"
- What it does: This commits your staged changes with a message describing what you changed.
- Push Changes to GitHub:
git push
-
What it does: This pushes your committed changes to the remote repository on GitHub. Since you’ve set the upstream branch earlier, you can just type
git push
without any additional arguments.
Now you know how to push your own repo online and handle future commits!
➤ Scenario: Contributing to Others' Repositories
Let’s say you want to contribute to someone else’s project. Here’s how you can do that step by step:
Fork the Repository: Go to the repository you want to contribute to and click on the "Fork" button. This creates a copy in your GitHub account.
Clone Your Fork:
git clone <Your_Fork_URL>
-
What it does: This command copies your forked repository to your local machine. Replace
<Your_Fork_URL>
with the URL of your fork.
- Navigate to Your Project Folder:
cd <Your_Repo_Name>
- What it does: This changes your terminal’s directory to your cloned repository folder.
Make Changes: Edit the files as needed.
Stage Your Changes:
git add .
- What it does: This stages your new changes.
- Commit Your Changes:
git commit -m "Your message"
- What it does: This commits your changes with a message.
- Push Changes to Your Fork:
git push
- What it does: This pushes your committed changes to your forked repository on GitHub.
Create a Pull Request: Go back to the original repository on GitHub and click on "Pull Requests." Then, click on "New Pull Request." Select your branch with changes and submit the pull request. This notifies the repository owner about your proposed changes.
-
Merge the Pull Request (if you're the owner of the repository):
- If your pull request is approved, you can merge it into the main branch of the original repository by clicking "Merge Pull Request." This incorporates your changes into the project.
And that’s it! You now know how to contribute to others' repositories on GitHub.
➤ Final Thoughts
Congratulations! 👏 You’ve taken your first steps into the world of Git and GitHub. If you want to dive deeper and learn advanced commands, here are some resources I found helpful:
Happy coding, and don’t hesitate to explore more.
Posted on September 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.