If you’re new to coding or software development, you’ve probably heard about Git and GitHub but might not fully understand what they are or how to use them. In this guide, I’ll walk you through the basics of Git, the most popular version control system, and GitHub, the online platform that hosts Git repositories. By the end, you’ll be ready to start managing your projects with Git and collaborating on GitHub like a pro.
What Are Git and GitHub?
Git is a distributed version control system. It helps you track changes in your files (usually code) over time, so you can revert back to previous versions if needed, and work on different features in parallel without conflicts.
GitHub is a cloud-based service that lets you store your Git repositories online. It provides collaboration tools such as pull requests, issue tracking, and more, making it easier for teams to work together on projects.
Step 1: Installing Git
Before you begin, you need Git installed on your computer.
- On Windows, download Git from git-scm.com and follow the installation instructions.
- On macOS, Git comes pre-installed on many systems, but you can update or install it using Homebrew: nginxCopyEdit
brew install git
- On Linux, install Git using your package manager, for example: arduinoCopyEdit
sudo apt-get install git
After installing, open your terminal (Command Prompt, Terminal, or Git Bash) and verify the installation:
git --version
Step 2: Configuring Git
Set your identity for commits using your name and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
This info will appear in your commits.
Step 3: Creating a New Git Repository
Navigate to your project folder or create a new one, then initialize Git:
mkdir my-project
cd my-project
git init
This command creates a hidden .git
folder which tracks your project history.
Step 4: Adding and Committing Files
Create a new file (e.g., index.html
) and add some content. Then check the status:
git status
You’ll see untracked files listed. Add files to the staging area:
git add index.html
Now commit your changes:
git commit -m "Add initial index.html file"
The commit message should be clear and concise, describing what you changed.
Step 5: Viewing Commit History
You can see your commit history with:
git log
Press q
to exit the log view.
Step 6: Creating a Repository on GitHub
- Go to GitHub and sign up for an account.
- Click the + icon and select New repository.
- Enter a repository name, description (optional), and choose public or private.
- Click Create repository.
Step 7: Linking Local Repository to GitHub
Once the GitHub repository is created, copy the HTTPS URL (e.g., https://github.com/username/my-project.git
).
Back in your terminal, add this remote URL:
git remote add origin https://github.com/username/my-project.git
Push your local commits to GitHub:
git push -u origin master
The -u
flag sets the remote origin
as the default upstream for future pushes.
Step 8: Cloning a Repository
To download an existing GitHub repo to your computer, use:
git clone https://github.com/username/repo-name.git
This creates a local copy you can work on.
Step 9: Working with Branches
Branches let you develop features without affecting the main codebase.
Create a new branch:
cssCopyEditgit checkout -b feature-branch
Switch back to master/main:
nginxCopyEditgit checkout master
Merge a branch after work is complete:
git merge feature-branch
Step 10: Pulling Changes from GitHub
To update your local repo with changes made by others, run:
git pull origin master
Final Tips
- Commit often with meaningful messages.
- Use branches for new features or bug fixes.
- Regularly push your commits to GitHub.
- Learn about
.gitignore
to exclude files from tracking.
Conclusion
Git and GitHub are invaluable tools for developers, enabling smooth collaboration and powerful version control. With this guide, you now have the basics to start using Git confidently. Keep practicing and exploring Git commands and GitHub features, and soon it will become second nature.