Git Tutor
Flow

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.

If you can open a terminal and copy/paste commands, you’re ready to learn Git.

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.

Why use Git?
  • Save your work with messages (commits).
  • Create branches to try ideas without breaking main code.
  • Share code with others via remotes like GitHub.
What Git is not:
  • 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

Windows

Download the official installer and accept defaults.

Download Git for Windows

macOS

brew install git

If you don’t have Homebrew, get it at brew.sh.

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
Use the same email you use on GitHub if you want your commits linked to your profile.

Your First Repository

Start a new local project

1
Create a folder and a README file.
mkdir my-project && cd my-project
echo "# My Project" > README.md
2
Initialize Git and create your first commit.
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
If there’s a conflict, Git will mark files. Open them, keep the correct lines, then add and commit.

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
If you amended or reset and already pushed, use 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}

Tags (Releases)

git tag v1.0.0                         # lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0"   # annotated tag
# Push a single tag
git push origin v1.0.0
# Push all tags
git push --tags

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

Feature branch + Pull Request
  1. Create a branch: git switch -c feature/thing
  2. Commit work; push branch: git push -u origin feature/thing
  3. Open a Pull Request; get review
  4. Merge to main and update local
Git Flow (summary)
  • main is production, develop for 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

  1. Create a repo and commit a file.
  2. Create a feature branch and make a change.
  3. Merge your feature into main.
  4. Add a tag and push it.
  5. Try a safe undo with git revert.
Practice makes permanent. Repeat until the flow feels effortless.