Welcome to this comprehensive Git and GitHub tutorial! This guide is designed to take you from the very basics of version control to advanced Git techniques, with step-by-step instructions and explanations.
- Introduction to Version Control
- Getting Started with Git
- Basic Git Commands
- Working with GitHub
- Branching and Merging
- Collaboration Workflows
- Advanced Git Techniques
- Best Practices
- Troubleshooting Common Issues
Version control is a system that records changes to files over time so that you can recall specific versions later. It allows you to:
- Track changes to your code
- Revert to previous versions if needed
- Collaborate with others without overwriting each other's work
- Maintain different versions of a project simultaneously
- Local Version Control Systems: Simple database on your local machine
- Centralized Version Control Systems (CVCS): Single server storing all versioned files (e.g., SVN)
- Distributed Version Control Systems (DVCS): Every user has a complete copy of the repository (e.g., Git)
- Distributed: Each developer has a complete copy of the repository
- Fast: Most operations are local, reducing network latency
- Data integrity: Git uses checksums to ensure data integrity
- Branching: Git's branching model is lightweight and powerful
- Open source: Free and widely adopted
- Windows: Download and install from git-scm.com
- macOS: Install via Homebrew with
brew install gitor download from git-scm.com - Linux: Use your distribution's package manager, e.g.,
sudo apt-get install git(Ubuntu/Debian)
After installation, set up your identity:
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "your.email@example.com"
# Check your settings
git config --list# Create a new directory
mkdir my-first-repo
cd my-first-repo
# Initialize a Git repository
git initGit has three main states that your files can reside in:
- Modified: You've changed the file but haven't committed it yet
- Staged: You've marked a modified file to go into your next commit
- Committed: The data is safely stored in your local database
# Check status of your repository
git status
# Add a file to staging area
git add filename.txt
# Add all files to staging area
git add .
# Remove a file from staging area
git reset filename.txt# Commit staged changes
git commit -m "Your commit message here"
# Commit all changes (skips staging)
git commit -a -m "Your commit message here"# View commit history
git log
# View compact history
git log --oneline
# View graphical representation
git log --graph --oneline --decorateGitHub is a web-based hosting service for Git repositories. It provides:
- A centralized location to store repositories
- Tools for collaboration (pull requests, issues, etc.)
- Project management features
- Social coding features
- Go to github.com
- Fill out the sign-up form
- Choose a free or paid plan
- Verify your email address
- Click the "+" icon in the top right corner
- Select "New repository"
- Fill in repository name and description
- Choose public or private
- Initialize with README (optional)
- Click "Create repository"
# Add remote repository
git remote add origin https://github.com/username/repository-name.git
# Verify remote
git remote -v
# Push your local repository to GitHub
git push -u origin main# Clone a repository
git clone https://github.com/username/repository-name.git
# Clone to a specific folder
git clone https://github.com/username/repository-name.git folder-nameA branch represents an independent line of development. It allows you to:
- Work on new features without affecting the main codebase
- Experiment with ideas
- Collaborate with others without conflicts
# List all branches
git branch
# Create a new branch
git branch branch-name
# Switch to a branch
git checkout branch-name
# Create and switch to a new branch
git checkout -b branch-name
# Delete a branch
git branch -d branch-name# Switch to the target branch (e.g., main)
git checkout main
# Merge another branch into current branch
git merge branch-name
# Abort a merge in case of conflicts
git merge --abortWhen Git can't automatically merge changes, you'll need to resolve conflicts manually:
- Git will mark the conflicted files
- Open the files and look for conflict markers (
<<<<<<<,=======,>>>>>>>) - Edit the files to resolve conflicts
- Add the resolved files with
git add - Complete the merge with
git commit
A pull request (PR) is a method of submitting contributions to a project:
- Fork the repository (on GitHub)
- Clone your fork locally
- Create a branch for your changes
- Make and commit your changes
- Push your branch to your fork
- Create a pull request on GitHub
# Add the original repository as "upstream"
git remote add upstream https://github.com/original-owner/original-repository.git
# Fetch changes from upstream
git fetch upstream
# Merge changes into your local main branch
git checkout main
git merge upstream/main
# Push the updated main to your fork
git push origin main- Centralized Workflow: Everyone works on the main branch
- Feature Branch Workflow: Each feature is developed in its own branch
- Gitflow Workflow: Strict branching model with dedicated branches for features, releases, and hotfixes
- Forking Workflow: Contributors fork the repository and submit pull requests
# Save changes temporarily
git stash
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply specific stash
git stash apply stash@{n}
# Remove most recent stash
git stash drop
# Apply and remove most recent stash
git stash pop# Rebase current branch onto another branch
git rebase branch-name
# Interactive rebase
git rebase -i HEAD~3 # Rebase last 3 commits# Apply a specific commit to current branch
git cherry-pick commit-hash# Create a lightweight tag
git tag tag-name
# Create an annotated tag
git tag -a v1.0 -m "Version 1.0"
# List tags
git tag
# Push tags to remote
git push origin --tags# View reference logs
git reflog
# Recover deleted branch
git checkout -b recovered-branch commit-hash# Add a submodule
git submodule add https://github.com/username/repository.git path/to/submodule
# Initialize and update submodules
git submodule update --init --recursive- Write clear, concise commit messages
- Use the imperative mood ("Add feature" not "Added feature")
- First line should be 50 characters or less
- Provide detailed explanation in the body if necessary
- Use a
.gitignorefile to exclude unnecessary files - Include a README.md with project information
- Document your code and workflows
- Commit often, push regularly
- Pull before you push
- Use branches for features and bug fixes
- Review code before merging
# Discard changes in working directory
git checkout -- filename
# Undo last commit but keep changes
git reset --soft HEAD^
# Undo last commit and discard changes
git reset --hard HEAD^
# Undo a public commit (creates a new commit)
git revert commit-hash# Amend the last commit
git commit --amend -m "New commit message"
# Force push (use with caution!)
git push -f origin branch-name- "fatal: refusing to merge unrelated histories": Use
git pull origin main --allow-unrelated-histories - "fatal: remote origin already exists": Use
git remote rm originbefore adding a new origin - "error: failed to push some refs": Pull changes first with
git pull origin main
This tutorial covers the fundamentals of Git and GitHub. As you practice these commands and concepts, you'll become more comfortable with version control and be able to manage your projects more effectively.
For more information, check out: