Learn the fundamentals of Git to track changes, collaborate with others, and manage your projects efficiently.
$ git init
Initialized empty Git repository in /project/.git/
$ git add .
$ git commit -m "Initial commit"
[main (root-commit) abc1234] Initial commit
3 files changed, 25 insertions(+)
$ git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Git is a distributed version control system that helps you track changes in your code and collaborate with others.
Git keeps track of all changes made to your code, allowing you to revert to previous versions if needed.
Multiple developers can work on the same project simultaneously without overwriting each other's changes.
Every change is recorded with who made it and when, creating a complete project history.
A repository (or repo) is a collection of files and folders that Git tracks. It contains the complete history of all changes.
A commit is a snapshot of your repository at a specific point in time. Each commit has a unique ID.
A branch is an independent line of development. The default branch is usually called "main" or "master".
A remote is a version of your repository hosted on the internet (like GitHub) that team members can access.
Understanding the basic Git workflow will help you use it effectively in your projects.
Your local files
Prepared changes
Committed changes
Edit files in your working directory. Git recognizes changed files as "modified".
Add modified files to the staging area to prepare them for committing.
Permanently store the staged changes in your local repository with a descriptive message.
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
$ git add index.html
$ git commit -m "Update homepage layout"
[main 1a2b3c4] Update homepage layout
1 file changed, 5 insertions(+)
These are the most commonly used Git commands you'll need to know.
git init
Initialize a new Git repository in the current directory
git clone <repository-url>
Create a local copy of a remote repository
git remote add origin <repository-url>
Connect your local repository to a remote
git add <file>
Stage a specific file for commit
git add .
Stage all changed files for commit
git commit -m "commit message"
Commit staged changes with a message
git push origin <branch-name>
Push commits to the remote repository
git status
Show the working tree status
git log
Show commit logs
git diff
Show changes between commits/staging
git show <commit-id>
Show details about a specific commit
git restore <file>
Discard changes in working directory
git reset <file>
Unstage a file while keeping changes
git reset --hard <commit-id>
Reset repository to a specific commit (caution!)
git revert <commit-id>
Create a new commit that undoes a previous commit
Branches allow you to work on different features or fixes simultaneously without affecting the main codebase.
git branch
List all local branches
git branch <branch-name>
Create a new branch
git checkout <branch-name>
Switch to a branch
git checkout -b <branch-name>
Create and switch to a new branch
git merge <branch-name>
Merge a branch into the current branch
git branch -d <branch-name>
Delete a branch
Create a new branch for each feature or bugfix. Merge back to main when complete.
A more structured approach with main, develop, feature, release, and hotfix branches.
Simpler than Git Flow, with just main and feature branches plus pull requests.
$ git checkout -b feature/new-header
Switched to a new branch 'feature/new-header'
# Make changes to files...
$ git add .
$ git commit -m "Add new header design"
[feature/new-header 1a2b3c4] Add new header design
2 files changed, 15 insertions(+)
$ git checkout main
Switched to branch 'main'
$ git merge feature/new-header
Updating d4e5f6a..1a2b3c4
Fast-forward
Conflicts occur when Git can't automatically merge changes. You'll need to manually resolve them.
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
# Open the file and look for conflict markers:
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature/new-header
# After resolving conflicts:
$ git add index.html
$ git commit
Git enables seamless collaboration between team members working on the same project.
git remote -v
List remote repositories
git fetch
Download changes from remote without merging
git pull
Fetch and merge changes from remote
git push
Upload local changes to remote
On platforms like GitHub, PRs let you propose changes and discuss them before merging into main.
Create a branch → Push changes → Open PR → Get reviews → Merge after approval.
Always pull the latest changes before starting work to avoid conflicts.
Never commit directly to main. Create a branch for each task.
Each branch should address a single feature or bugfix.
Small, frequent commits are easier to review and revert if needed.
$ git pull origin main
Already up to date.
$ git checkout -b fix/login-bug
Switched to a new branch 'fix/login-bug'
# Make changes and commit...
$ git add .
$ git commit -m "Fix login validation bug"
$ git push origin fix/login-bug
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 489 bytes | 489.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Create a pull request for 'fix/login-bug' on GitHub by visiting:
remote: https://github.com/user/repo/pull/new/fix/login-bug
Write clear, concise messages that explain what changed and why.
Each commit should represent a single logical change.
Frequently pull changes from main to stay up-to-date.
Use consistent naming like feature/xxx, fix/xxx, or bugfix/xxx.
Always review code before merging to maintain quality.
Exclude unnecessary files (node_modules, IDE files, etc.).