Git and GitHub Basics: A Beginner’s Tutorial
Whether you’re a developer, data scientist, or student learning to code, understanding Git and GitHub is essential. They are the backbone of modern version control and collaboration in software projects. This guide will walk you through the basics of Git and how to use GitHub effectively.
What is Git?
Git is a version control system that tracks changes in your files and allows multiple people to work on a project without overwriting each other’s work.
Key Features:
- Track and manage code history
- Revert to previous versions
- Collaborate with others efficiently
- Branching and merging for parallel development
What is GitHub?
GitHub is a cloud-based platform that hosts your Git repositories. It allows teams to collaborate on code, review pull requests, and manage projects efficiently.
It provides:
- Remote repositories
- Collaboration tools (pull requests, issues, etc.)
- GitHub Actions (for automation)
- Community and open-source access
Getting Started with Git and GitHub
1. Install Git
Download Git from the official site:
https://git-scm.com/
Verify installation:
git --version
2. Set Up Git
git config --global user.name "Your Name"
git config --global user.email "your@example.com"
This sets your identity for commits.
3. Create a Local Repository
mkdir my-project
cd my-project
git init
This initializes a new Git repository in the folder.
4. Add and Commit Files
touch index.html
git add index.html # Stage the file
git commit -m "Initial commit" # Commit with a message
5. Create a Repository on GitHub
- Go to https://github.com
- Click New Repository
- Name it (e.g.,
my-project
) - Click Create Repository
6. Link Local Repo to GitHub
git remote add origin https://github.com/yourusername/my-project.git
git branch -M main
git push -u origin main
7. Cloning a Repository
If someone else shares a repo with you:
git clone https://github.com/username/repo-name.git
8. Basic Git Workflow
git pull # Get latest changes
git add . # Stage all changes
git commit -m "Update"
git push # Send changes to GitHub
Branching and Merging
Branching lets you work on features independently.
git checkout -b feature-branch
# work on code
git add .
git commit -m "Add feature"
git checkout main
git merge feature-branch
git push
Common Git Commands
Command | Description |
---|---|
git status |
Show changed files |
git log |
View commit history |
git diff |
View file changes |
git stash |
Temporarily save changes |
git reset |
Undo commits or staged files |
git rm |
Remove files |
Collaborating on GitHub
- Fork a repository
- Clone your fork locally
- Create a new branch and make changes
- Push the branch and create a Pull Request
- Collaborators review and merge your PR
Tips for Beginners
- Commit often with meaningful messages
- Keep your branches small and focused
- Always pull before you push
- Use
.gitignore
to avoid committing unnecessary files
Conclusion
Git and GitHub are essential tools in every developer’s toolkit. Once you understand the basic workflow, you’ll be able to manage projects, collaborate on code, and contribute to open-source software with confidence.