Learn Git and GitHub basics with this complete tutorial. Covers version control, repositories, commits, branches, merging, and collaboration. Ideal for beginners, developers, and AI enthusiasts to manage code efficiently and work in teams.
1. Introduction to Git & GitHub
Git is a version control system that tracks changes in your code. GitHub is a cloud platform for hosting Git repositories, enabling collaboration with other developers.
Benefits:
- Track code changes over time.
- Revert to previous versions if needed.
- Collaborate with other developers.
- Work on multiple features simultaneously using branches.
Best Practices:
- Commit small, meaningful changes.
- Write clear commit messages.
- Regularly push changes to remote repositories.
2. Installing Git
- Windows / Mac / Linux: Download from git-scm.com
- Verify installation:
git --version
3. Configuring Git
Set your name and email (used for commits):
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Check configuration:
git config --list
4. Basic Git Commands
Initialize a Repository
git init
Creates a local Git repository.
Check Status
git status
Add Files
git add filename.txt
git add .
Commit Changes
git commit -m "Add initial project files"
View Commit History
git log
Best Practices:
- Commit frequently to save progress.
- Use descriptive commit messages like
"Fix login bug"or"Add data preprocessing script".
5. Branching & Merging
Branches allow multiple features to be developed simultaneously.
Create a Branch
git branch feature-login
git checkout feature-login
Or combine into one command:
git checkout -b feature-login
Merge Branches
git checkout main
git merge feature-login
Best Practices:
- Keep
mainbranch stable. - Work on separate branches for new features or bug fixes.
- Regularly merge branches to avoid conflicts.
6. GitHub Basics
GitHub hosts your Git repositories online for collaboration.
Create a Repository
- Sign up / log in to GitHub.
- Click New Repository, enter name, description, and visibility.
Push Local Repo to GitHub
git remote add origin https://github.com/username/repo.git
git branch -M main
git push -u origin main
Clone Repository
git clone https://github.com/username/repo.git
Pull Changes
git pull origin main
Best Practices:
- Use meaningful repository names.
- Add a README.md to describe the project.
- Use
.gitignoreto exclude unnecessary files.
7. Collaboration Workflow
- Fork a repository.
- Clone it locally.
- Create a branch for your changes.
- Commit and push changes.
- Open a Pull Request on GitHub.
Best Practices:
- Review pull requests carefully.
- Communicate changes clearly in PR descriptions.
- Always sync your fork with the main repository regularly.
8. Summary & Best Practices
- Git tracks code changes; GitHub hosts repositories online.
- Use branches for features and bug fixes.
- Commit frequently with meaningful messages.
- Push and pull changes to keep code synced with the team.
- Collaborate efficiently using forks and pull requests.
By mastering Git & GitHub, beginners can manage projects effectively, track changes, and collaborate with other developers in AI, software, and web projects.