Basics of Git and GitHub
Introduction
Welcome to the world of Git and GitHub! Whether you’re new to coding or just want to get a handle on version control, this guide will walk you through the basics. By the end, you'll be able to manage your code like a pro.
What is Git?
Git is a version control system. It helps you keep track of changes in your code, collaborate with others, and roll back to previous versions if something goes wrong. Think of it as a time machine for your code.
What is GitHub?
GitHub is a platform that uses Git. It allows you to store your code online, share it with others, and collaborate on projects. It's like a social network for developers.
Getting Started
Before we dive in, you’ll need to install Git on your computer and create a GitHub account.
Installing Git
Windows:
Download Git from git-scm.com.
Run the installer and follow the prompts.
Mac:
Open Terminal.
Install Git using Homebrew:
brew install git
.
Linux:
- Use your package manager, for example, on Ubuntu:
sudo apt-get install git
.
- Use your package manager, for example, on Ubuntu:
Creating a GitHub Account
Go to github.com.
Click “Sign up” and fill in the required details.
Basic Git Commands
Let’s start with some basic Git commands.
Configuring Git
Set up your Git username and email:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Initializing a Repository
A repository (repo) is a project that Git tracks.
mkdir my-project
cd my-project
git init
Adding Files
Create a new file in your project folder and write some code. Then, tell Git to track this file.
git add yourfile.txt
Committing Changes
A commit is like a snapshot of your project at a point in time.
git commit -m "Add initial file"
Working with GitHub
Now that you have a local repository, let’s connect it to GitHub.
Creating a Repository on GitHub
Go to your GitHub account.
Click the “+” icon in the top-right corner and select “New repository”.
Name your repository and click “Create repository”.
Connecting Your Local Repo to GitHub
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin master
Collaboration with GitHub
Cloning a Repository
To work on someone else’s project, you need to clone their repository.
git clone https://github.com/username/repo.git
Branching
Branches allow you to work on different parts of a project simultaneously.
git branch new-feature
git checkout new-feature
Merging
Once your feature is ready, merge it back into the main branch.
git checkout master
git merge new-feature
Pull Requests
A pull request lets others know you’ve made changes to their repository and would like to merge them.
Push your branch to GitHub:
git push origin new-feature
Go to the repository on GitHub.
Click “Compare & pull request” and submit your pull request.
Keeping Your Fork Updated
If you fork a repository (copy someone else’s project), keep it updated with the original repository.
git remote add upstream https://github.com/originalowner/repo.git
git fetch upstream
git merge upstream/master
Basic Workflow
Clone the repository.
Create a branch for your feature.
Make changes and commit them.
Push the branch to GitHub.
Create a pull request.
Conclusion
Congratulations! You've learned the basics of Git and GitHub. Practice these commands, and soon, you'll be managing your code like a pro. Remember, the key to mastering Git is practice and using it in real projects. Happy coding!
Resources
Subscribe to my newsletter
Read articles from Ayesha Saher directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by