Git & GitHub Tutorial: Beginner to Advanced

Master version control, collaboration, and advanced workflows with this in-depth Git & GitHub guide for all levels.

Table of Contents

Introduction

Git is a distributed version control system that helps you track changes in your code and collaborate with others. GitHub is a cloud-based hosting service for Git repositories, making it easy to share, review, and manage code with teams or the open source community.

Who is this guide for?
Anyone who wants to learn Git & GitHub, from absolute beginners to advanced users looking for best practices and advanced workflows.

1. Git Basics (Beginner)

1.1. Installing Git

1.2. Git Configuration

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "code --wait"   # Use VSCode as editor

1.3. Creating a Repository

git init my-project
cd my-project

This creates a new folder my-project with a Git repository.

1.4. Basic Workflow

  1. Edit or add files in your project directory.
  2. Stage changes: git add filename or git add .
  3. Commit changes: git commit -m "Describe your changes"
  4. View history: git log

1.5. Cloning a Repository

git clone https://github.com/username/repo.git

1.6. Checking Status

git status

1.7. Ignoring Files

Create a .gitignore file to exclude files/folders from version control.

node_modules/
.env
*.log

2. GitHub Basics (Beginner)

2.1. Creating a GitHub Account

Sign up at github.com.

2.2. Creating a Repository on GitHub

  1. Click New on your GitHub dashboard.
  2. Fill in repository name, description, and choose public/private.
  3. Click Create repository.

2.3. Connecting Local Repo to GitHub

git remote add origin https://github.com/username/repo.git
git push -u origin main   # or master

2.4. Forking & Pull Requests

  1. Fork a repo on GitHub.
  2. Clone your fork, make changes, push to your fork.
  3. On GitHub, click New pull request to propose your changes.

2.5. Collaborators & Permissions

3. Intermediate Git & GitHub

3.1. Branching & Merging

git branch feature-x         # Create branch
git checkout feature-x      # Switch to branch
# or: git switch feature-x
git merge feature-x         # Merge into current branch
git branch -d feature-x     # Delete branch
Tip: Use branches for new features, bug fixes, or experiments.

3.2. Resolving Merge Conflicts

  1. Git will mark conflicts in files.
  2. Edit files to resolve conflicts, then:
git add conflicted-file
git commit

3.3. Stashing Changes

git stash           # Save uncommitted changes
git stash pop       # Restore them later

3.4. Undoing Changes

3.5. Working with Remotes

git remote -v                # List remotes
git remote add upstream URL  # Add another remote
git fetch upstream           # Fetch changes
git merge upstream/main      # Merge upstream changes

4. Advanced Git & GitHub

4.1. Rebase

git checkout feature-x
git rebase main

Rebase rewrites commit history for a cleaner project history.

4.2. Interactive Rebase

git rebase -i HEAD~5

Squash, edit, or reorder commits interactively.

4.3. Cherry-pick

git cherry-pick <commit-hash>

Apply a specific commit from another branch.

4.4. Tags & Releases

git tag v1.0.0
git push origin v1.0.0

Tags mark specific points in history (e.g., releases).

4.5. Submodules

git submodule add https://github.com/other/repo.git path/to/submodule
git submodule update --init --recursive

Include other repositories inside your repo.

4.6. GitHub Actions (CI/CD)

4.7. Security & Secrets

5. Tips, Best Practices & Resources

6. FAQ

Q: What is the difference between Git and GitHub?
A: Git is a version control tool; GitHub is a platform for hosting and collaborating on Git repositories.

Q: What is a pull request?
A: A request to merge your changes into another branch or repository, often used for code review.

Q: How do I undo a commit?
A: Use git revert <commit> to create a new commit that undoes changes, or git reset for local history changes.

Q: How do I contribute to open source?
A: Fork the repo, make changes, push to your fork, and open a pull request.

Q: How do I resolve merge conflicts?
A: Edit the conflicted files, remove conflict markers, then git add and git commit.