Version control is one of the most important skills a developer can learn. Whether you are working on a solo project or collaborating with a team of fifty, Git tracks every change to your code, lets you revert mistakes, and enables multiple people to work on the same project without overwriting each other's work. GitHub builds on top of Git by providing a web-based platform for hosting repositories, reviewing code, and managing projects. This guide covers everything you need to get started with both.
What Is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. It records snapshots of your project at specific points in time (called commits), allowing you to view the complete history of changes, compare versions, and roll back to any previous state. Unlike older systems, Git is distributed, meaning every developer has a full copy of the entire project history on their local machine.
What Is GitHub?
GitHub is a cloud-based hosting platform for Git repositories. It adds features on top of Git such as pull requests, issue tracking, project boards, actions for CI/CD, and a social layer where you can follow developers and star projects. While Git works entirely on your local machine, GitHub provides the remote server that enables collaboration and backup.
Alternatives to GitHub include GitLab and Bitbucket, but GitHub is by far the most widely used platform, with over 100 million developers as of 2026.
Installing Git
Windows
Download the installer from git-scm.com and run it. The installer presents many options; the defaults are fine for most users. Make sure "Git from the command line and also from 3rd-party software" is selected so you can use Git from Command Prompt and PowerShell.
macOS
Open Terminal and type git --version. If Git is not installed, macOS will prompt you to install the Xcode Command Line Tools, which include Git. Alternatively, install it via Homebrew with brew install git.
Linux
Use your distribution's package manager:
# Debian/Ubuntu
sudo apt install git
# Fedora
sudo dnf install git
Initial Configuration
After installing Git, configure your identity. These details are attached to every commit you make:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
You can also set your default branch name to main (the modern convention):
git config --global init.defaultBranch main
Basic Git Workflow
The fundamental Git workflow consists of three stages: the working directory (where you edit files), the staging area (where you prepare changes for a commit), and the repository (where committed snapshots are stored).
Creating a Repository
Navigate to your project folder and initialize Git:
mkdir my-project
cd my-project
git init
This creates a hidden .git folder that stores all version control data. Your project is now a Git repository.
Checking Status
The git status command shows you which files have been modified, which are staged for commit, and which are untracked:
git status
Get into the habit of running this command frequently. It is your window into what Git sees.
Staging Changes
Before you can commit changes, you need to stage them. This tells Git which changes to include in the next commit:
# Stage a specific file
git add index.html
# Stage all changed files
git add .
Committing Changes
A commit saves a snapshot of all staged changes along with a descriptive message:
git commit -m "Add homepage layout and navigation bar"
Write commit messages that describe what was changed and why. Good commit messages make the project history useful for everyone on the team.
Viewing Commit History
# Full log
git log
# Compact one-line format
git log --oneline
Working with GitHub
Creating a GitHub Account
Go to github.com and sign up for a free account. Choose a username that you will be comfortable using professionally, as your GitHub profile often functions as a developer portfolio.
Creating a Repository on GitHub
- Click the + icon in the top-right corner and select New repository.
- Give it a name (e.g.,
my-project), add an optional description, and choose public or private visibility. - You can optionally initialize with a README, .gitignore, and license, but if you already have a local repo, leave these unchecked.
- Click Create repository.
Connecting a Local Repo to GitHub
After creating the remote repository, GitHub shows you the commands to link your local repository:
git remote add origin https://github.com/yourusername/my-project.git
git branch -M main
git push -u origin main
The -u flag sets up tracking so that future git push and git pull commands know which remote branch to use.
Cloning an Existing Repository
To download someone else's repository (or your own from another machine):
git clone https://github.com/username/repository-name.git
This creates a local copy of the repository with its full history.
Branching and Merging
Branches are one of Git's most powerful features. They let you work on new features or bug fixes in isolation without affecting the main codebase.
Creating and Switching Branches
# Create a new branch
git branch feature-login
# Switch to it
git checkout feature-login
# Or create and switch in one command
git checkout -b feature-login
Merging Branches
Once your feature is complete, merge it back into the main branch:
git checkout main
git merge feature-login
If there are no conflicting changes, Git performs a clean merge. If conflicts exist, Git marks the conflicting sections in the affected files and you must resolve them manually before completing the merge.
Deleting a Branch
After merging, clean up by deleting the feature branch:
git branch -d feature-login
Pull Requests on GitHub
In a collaborative workflow, you rarely merge branches locally. Instead, you push your branch to GitHub and create a pull request (PR). A pull request is a proposal to merge your changes into another branch, and it provides a space for code review and discussion.
- Push your feature branch to GitHub:
git push origin feature-login - Go to the repository on GitHub. You will see a prompt to create a pull request for your recently pushed branch.
- Click Compare & pull request, write a description of your changes, and submit.
- Team members review the code, leave comments, and approve or request changes.
- Once approved, click Merge pull request to merge the changes into the main branch.
The .gitignore File
Not every file should be tracked by Git. Compiled files, dependency folders, environment variables, and OS-generated files should be excluded. Create a .gitignore file in your project root:
# Dependencies
node_modules/
# Environment variables
.env
.env.local
# Build output
dist/
build/
# OS files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/
GitHub provides templates for common languages and frameworks at github.com/github/gitignore.
Pulling Changes
When collaborating, other team members push changes to the remote repository. To fetch and merge those changes into your local branch:
git pull origin main
Always pull before you start new work to make sure you are building on the latest version of the code.
Common Git Workflows
Feature Branch Workflow
This is the most popular workflow for teams. Every new feature or bug fix gets its own branch created from main. Work is done on the feature branch, pushed to GitHub, reviewed via a pull request, and merged into main. The feature branch is then deleted.
Solo Developer Workflow
Even when working alone, Git is valuable. Use main for stable code and create branches for experiments. Commit frequently with meaningful messages so you can always roll back if something breaks.
Resolving Merge Conflicts
Merge conflicts happen when two branches modify the same part of the same file. Git cannot decide which change to keep, so it marks the conflicting sections and asks you to resolve them manually. A conflict looks like this in your file:
<<<<<<< HEAD
Your changes on the current branch
=======
Changes from the branch being merged
>>>>>>> feature-branch
To resolve the conflict, open the file in your editor, choose which version to keep (or combine both), remove the conflict markers, and then stage and commit the file:
git add conflicted-file.html
git commit -m "Resolve merge conflict in conflicted-file.html"
VS Code has a built-in merge conflict editor that highlights conflicts and provides clickable buttons to accept one side or the other, making resolution much easier.
Undoing Mistakes
One of Git's greatest strengths is the ability to undo mistakes. Here are the most common scenarios:
- Undo unstaged changes:
git checkout -- filenamerestores a file to its last committed state. - Unstage a file:
git reset HEAD filenameremoves a file from the staging area without discarding your changes. - Amend the last commit:
git commit --amendlets you update the last commit message or add forgotten files. - Revert a commit:
git revert <commit-hash>creates a new commit that undoes the changes from a specific commit, preserving history.
Avoid using git reset --hard unless you are absolutely sure, as it permanently discards uncommitted changes.
Essential Commands Cheat Sheet
git init– Initialize a new repositorygit clone <url>– Clone a remote repositorygit status– Check the current state of your repogit add <file>– Stage changes for commitgit commit -m "message"– Save staged changes with a messagegit push– Upload local commits to the remotegit pull– Download and merge remote changesgit branch <name>– Create a new branchgit checkout <branch>– Switch to a branchgit merge <branch>– Merge a branch into the current branchgit log --oneline– View compact commit historygit diff– Show unstaged changesgit stash– Temporarily save uncommitted changes
Conclusion
Git and GitHub are foundational tools that every developer should learn early in their career. Git gives you the safety net of version control, while GitHub provides a platform for collaboration and code sharing. Start by using the basic workflow of add, commit, and push, then gradually incorporate branches, pull requests, and more advanced features as your confidence grows. Once these tools become second nature, you will wonder how you ever coded without them.