Welcome
You will learn Git step-by-step, starting from zero. Follow the sections in order and try the commands. Use the “Copy” buttons to run commands in your terminal.
What is Git?
Git is a tool that remembers every version of your files. It helps you save changes, go back in time, work on new ideas, and collaborate safely.
- Save your work with messages (commits).
- Create branches to try ideas without breaking main code.
- Share code with others via remotes like GitHub.
- Not a cloud by itself (GitHub/GitLab/Bitbucket are hosting).
- Not automatic backups—you still push to a remote.
Visual Overview
These diagrams show how Git concepts connect. Start with the version flow, then see a typical feature branch workflow.
Version Flow
Feature Branch Workflow
Add your own image
Place an image (e.g., git-overview.png) in the same
folder and embed it where you like:
<img src="git-overview.png" alt="Git overview diagram" width="900" />
Install Git
Linux
sudo apt-get install git # Debian/Ubuntu
sudo dnf install git # Fedora
sudo pacman -S git # Arch
First-time Setup
Run these once so Git knows your name, email, default branch, and editor.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
# Optional
git config --global core.editor "code --wait" # VS Code as editor
Your First Repository
Start a new local project
mkdir my-project && cd my-project
echo "# My Project" > README.md
git init
git add . Or git add file-name (option)
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/user/repo.git
git push -u origin main
Clone an existing project
git clone https://github.com/user/repo(file-name).git
cd repo (file name)
git init
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
- if error
git remote add origin https://github.com/user/repo(file-name).git
git branch -M main
git push -u origin main
git status
git branch
git checkout (to switch branch to other branch)
git checkout -b branch name (to create new branch)
git branch -d branch name (to delete branch)
git remote -v
git add remote origin your repo link(https://github.com/Gongchampou/GIT-help.git)
git branch -M main
git push -u origin main
git diff (to check the branch changes)
git merge branch name eg (main)
git push origin branch name to push and ask for pull and Request
git pull origin main(to get the fresh changes)
git fetch origin
rm file name (delete any file)
remove-item -recurse -force .git (to delete git file inside the folder so that it wont connect and give problem when thing change)
Resume after connecting repo and local storage
git add . or git add file-name (option)
git commit -m 'update massage'
git push origin main
git pull origin main --rebase
git push origin main
Cloning downloads a full copy of the project history.
Everyday Basics
See what changed
git status
git diff # unstaged changes
git diff --staged # staged changes
Stage and commit
git add file1 file2 # or: git add .
git commit -m "Describe the change"
# Quick add & commit for tracked files
git commit -am "Message"
View history
git log --oneline
# Pretty graph view
git log --oneline --graph --decorate --all
.gitignore
node_modules/
.env
.DS_Store
*.log
build/
dist/
Branching
Branches let you work on a new idea without affecting the main code.
git branch # list branches
git switch -c feature/login # create and switch
# older syntax:
# git checkout -b feature/login
Merge a branch
git switch main
git pull # update main first
git merge feature/login
Remotes (GitHub, GitLab, etc.)
Connect your local repository to an online host.
# Add a remote named "origin"
git remote add origin https://github.com/user/repo.git
# First push for a branch
git push -u origin main
# Pull (fetch + merge)
git pull
# Just download latest refs without merging
git fetch
Undo & Fix Mistakes
Common fixes
# Discard local (unstaged) changes
git restore path/to/file
# Unstage a file
git restore --staged path/to/file
# Edit the last commit
git commit --amend
git push --force-with-lease. Avoid this on shared
branches; prefer revert.
Go back safely
# Create a new commit that undoes a commit
git revert <commit_sha>
Move HEAD back (be careful)
# Relative to HEAD
git reset --soft HEAD~1 # keep changes staged
git reset --mixed HEAD~1 # keep changes unstaged
git reset --hard HEAD~1 # discard changes
# Reset to a specific commit by ID (SHA)
git reset --soft <commit_sha>
git reset --mixed <commit_sha>
git reset --hard <commit_sha>
Stash (Save Work-in-Progress)
git stash push -m "WIP: message"
git stash list
git stash show -p stash@{0}
# apply and drop
git stash pop
# or keep it:
git stash apply stash@{0}
Rebase (Rewriting History)
Rebase moves your commits onto a new base. Use it to keep a clean history. Avoid rebasing shared branches without coordination.
# Update your feature branch with latest main safely
git switch feature/login
git fetch origin
git rebase origin/main
# If conflicts: fix files, then
# git add . && git rebase --continue
# Abort if needed
# git rebase --abort
Cherry-pick
Apply a specific commit from anywhere onto your current branch.
git switch hotfix/urgent
# Apply a single commit by SHA
git cherry-pick <commit_sha>
SSH Keys (No password pushing)
Create a new SSH key
ssh-keygen -t ed25519 -C "you@example.com"
# Press Enter to accept defaults, then set a passphrase
Start the agent and add your key
# Windows PowerShell
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
# macOS/Linux
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Add the public key to GitHub
Copy your public key and paste it into GitHub → Settings → SSH and GPG keys → New SSH key.
# Windows
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
# macOS/Linux
cat ~/.ssh/id_ed25519.pub
Workflows
-
Create a branch:
git switch -c feature/thing -
Commit work; push branch:
git push -u origin feature/thing - Open a Pull Request; get review
- Merge to
mainand update local
-
mainis production,developfor integration - Feature, release, and hotfix branches for structured work
Troubleshooting
Common issues
- Permission denied (publickey): Add SSH key to GitHub and agent.
- Detached HEAD: Switch to a branch or create one from current commit.
- Rejected push: Pull or rebase first, or force-with-lease if you rewrote history.
Helpful inspection
git remote -v
git log --oneline --graph --decorate --all
git status
git reflog
Glossary
- Repository (repo): A project tracked by Git.
- Commit: A saved snapshot of changes.
- Branch: A movable pointer to a line of development.
- Remote: A reference to a hosted copy, e.g., GitHub.
- Merge: Combine changes from one branch to another.
- Rebase: Reapply commits on top of another base.
- Tag: A named pointer for versions/releases.
- Stash: Temporarily save uncommitted work.
Practice: Your First End-to-End Flow
- Create a repo and commit a file.
- Create a feature branch and make a change.
- Merge your feature into
main. - Add a tag and push it.
- Try a safe undo with
git revert.