Teaching AI visually and practically!
This tutorial provides a comprehensive guide to using Git and GitHub, covering everything from basic concepts to advanced techniques. Whether you’re a beginner or looking to deepen your understanding, this guide will walk you through version control, collaboration, and best practices with clear explanations, examples, and code snippets.
Version control is a system that tracks changes to files, allowing multiple people to collaborate on a project while maintaining a history of changes. It enables you to revert to previous versions, track who made changes, and work on different features simultaneously.
Git is a distributed version control system designed to handle everything from small to large projects with speed and efficiency. It tracks changes locally on your machine and supports collaboration through remote repositories.
GitHub is a platform for hosting Git repositories, enabling collaboration, code sharing, and project management. It adds features like pull requests, issues, and CI/CD pipelines on top of Git.
Git | GitHub |
---|---|
Local VCS | Cloud platform |
CLI Tool | Web UI & Hosting |
Offline | Online features |
Download and install Git from git-scm.com. Verify installation:
git --version
Configure your username and email for commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git init
git clone https://github.com/username/repository.git
git status
git add filename.txt
git add . # Stages all changes
git commit -m "Add initial project files"
git log
git diff
Branches allow you to work on different features or fixes independently. The default branch is usually main
.
git branch feature-branch
git checkout feature-branch
git checkout -b feature-branch
Merge changes from one branch into another:
git checkout main
git merge feature-branch
If changes conflict, Git will pause the merge. Open the conflicting files, resolve the conflicts (marked with <<<<<<<
, =======
, >>>>>>>
), then:
git add resolved-file.txt
git commit
Link your local repository to a remote on GitHub:
git remote add origin https://github.com/username/repository.git
Send local commits to the remote repository:
git push origin main
Retrieve and merge changes from the remote:
git pull origin main
Download changes without merging:
git fetch origin
Remove a remote connection:
git remote remove origin
Sign up at github.com and verify your email.
git remote add origin https://github.com/username/repository.git
git push -u origin main
Fork a repository by clicking “Fork” on GitHub to create your own copy.
Use GitHub Issues to report bugs or request features. Assign labels, milestones, or assignees.
git reset HEAD~1
git revert <commit-hash>
Modify the last commit:
git commit --amend
Reapply commits on top of another base:
git rebase main
Apply a specific commit to another branch:
git cherry-pick <commit-hash>
Temporarily save uncommitted changes:
git stash
git stash pop
Mark a specific commit (e.g., for a release):
git tag v1.0.0
git push origin v1.0.0
Clean untracked files:
git clean -f
A .gitignore
file specifies which files or directories Git should ignore (e.g., temporary files, sensitive data).
Example .gitignore
:
node_modules/
dist/
.env
*.log
Automate workflows (e.g., CI/CD). Example workflow file (.github/workflows/ci.yml
):
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: npm test
Host static websites directly from a repository. Enable in repository settings.
Engage with the community through forum-like discussions.
Document your project with a built-in wiki.
Enable Dependabot to monitor and update dependencies for security vulnerabilities.
Write clear, concise messages:
Add user authentication endpoint
main
, develop
, and feature branches.main
and short-lived feature branches.Review PRs for quality, functionality, and style.
Regularly prune old branches and archive stale repositories.
Manually edit conflicting files, then mark as resolved:
git add resolved-file.txt
git commit
Recover a deleted branch:
git branch feature-branch <commit-hash>
Generate and add an SSH key:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
cat ~/.ssh/id_rsa.pub # Add to GitHub
Official Documentation:
Cheat Sheet
Community:
Books:
Courses:
Happy coding! 🚀 Start your version-controlled journey today. If you found this helpful, ⭐ the repo and share!
Next Topic: - Mathematics for AI
Need More Help: Ask Your Questions