Why Git?
Imagine working on a project for weeks. You make hundreds of changes, add new features, fix bugs, and then suddenly something breaks.
Git acts like a version history for your project.
It tracks changes to your code and allows you to:
Go back to previous versions of your code.
Work on different features using branches.
Collaborate with other developers without overwriting their work.
Merge different features into the main project.
See exactly what changed and when.
Recover accidentally deleted or modified files.
GitHub builds on top of Git by providing a remote place to store repositories and collaborate with other developers.
Basic Git Commands
Configure Git
Set your name and email:
Check your configuration:
Create a Repository
Go to your project directory:
Initialize Git:
Check the repository status:
Add and Commit Changes
Add all changed files to the staging area:
Or add a specific file:
Create a commit:
Think of a commit as a snapshot of your project at that point in time.
Connect to GitHub
Add your GitHub repository as a remote:
Check the remote:
Push your code to GitHub:
After setting the upstream branch, you can simply use:
Clone a Repository
To download an existing repository from GitHub:
Clone a specific branch:
Git Branches
Branches allow you to work on a feature or fix without directly modifying the main codebase.
For example:
Create and switch to a new branch:
Switch to an existing branch:
List branches:
List local and remote branches:
Delete a local branch:
Delete a remote branch:
Merge Branches
First, switch to the branch you want to merge into:
Then merge the feature branch:
Pull Changes from GitHub
Get the latest changes from the remote repository:
git pull essentially combination of: git fetch + git merge
Fetch downloads the latest remote changes without merging them, while merge combines them with your local branch.
Safely Sync Local and Remote Changes:
Before pulling, check what changed on both sides:
Check commits coming from remote:
Check your local commits not pushed yet:
Check file differences:
Check for uncommitted changes:
If you have local changes you want to protect:
Then choose how to sync.
Keep your local commits and apply them on top of remote:
Restore your stashed changes if needed:
Discard local differences and match the remote:
Use
reset --hardcarefully - it discards local tracked changes. Creating a backup branch first gives you a way to recover your previous committed state.
Undo Changes
Undo the last commit but keep the changes
Undo the last commit and discard the changes
Be careful with
--hardbecause it can permanently discard local changes.
Undo a commit safely
Instead of removing a commit from history, create a new commit that reverses it:
Modify the last commit
Restore Deleted Files
If a file was deleted but the deletion hasn't been committed:
To restore a file from an older commit:
View the history of a specific file:
Forking a Repository
A fork is a copy of someone else's GitHub repository under your own GitHub account.
It's commonly used when contributing to open-source projects where you don't have direct write access.
The usual workflow is:
Clone your fork:
The Git Commands I Use Most
If you don't want to memorize everything, start with these:
These commands cover most of the day-to-day Git workflow.
Git is basically the tool that manages your project's history; GitHub is where you can store that history online and collaborate with others.
