Basic GitHub tutorial for developers
Git and GitHub Tutorial for Beginners
Starting with Git can feel confusing at first, but once you understand a few core commands, it becomes one of the most useful tools for managing code. This guide explains Git and GitHub in a simple way, with clear examples you can follow step by step.
What is Git
Git is a version control system. It helps you track changes in your code over time.
You can use Git to:
- save code history
- see what changed
- go back to older versions
- work safely with other developers
- avoid overwriting each other’s code
What is GitHub
GitHub is a website where developers store and manage Git repositories online.
Git does the tracking on your computer. GitHub gives you an online place to keep the project, share it, and collaborate with others.
Basic Terms You Should Know
Repository
A repository, or repo, is your project folder that Git tracks.
Commit
A commit is a saved snapshot of your changes.
Branch
A branch is a separate line of work. You can build a feature in a branch without touching the main code.
Clone
Clone means downloading a GitHub repository to your computer.
Push
Push means sending your local commits to GitHub.
Pull
Pull means getting the latest updates from GitHub to your computer.
Staging
Staging means preparing files before making a commit.
Git File Status
When you work with files in Git, they usually appear in one of these states:
- Untracked = new file that Git is not tracking yet
- Modified = tracked file has been changed
- Staged = file is ready to be committed
- Unmodified = no changes made since the last commit
Step 1: Install Git on Windows
First, install Git on your computer.
- Open your browser
- Go to https://git-scm.com/downloads
- Download Git for Windows
- Run the installer
- Complete the installation with default settings
After installing, open Command Prompt, PowerShell, or Git Bash and check the version:
git --version
If Git is installed correctly, you will see the installed version.
Step 2: Configure Git for the First Time
Before using Git, set your name and email. This information is attached to your commits.
git config --global user.name "RIZWAN AKHTAR"
git config --global user.email "rizikmw@gmail.com"
To view your saved Git configuration:
git config --list
Tip: Use the same email that you use on GitHub if you want your commits linked to your GitHub account.
Step 3: Clone a Repository from GitHub
If a project already exists on GitHub, you can download it to your local machine using git clone.
git clone <repository-link>
Example:
git clone https://github.com/RizwanKMW/reviewsfunda.git
This command creates a copy of the project on your computer.
Step 4: Check Repository Status
You can check the current state of your project files with:
git status
This command shows:
- which files changed
- which files are staged
- which files are not being tracked yet
Step 5: Add Files to Staging
Before saving changes in Git, you need to add them to the staging area.
Add one specific file
git add index.html
Add all changed files
git add .
Example: If you edited index.html and style.css, then git add . will stage both files.
Step 6: Commit Changes
After staging files, save them with a commit.
git commit -m "Add initial files"
A good commit message should clearly explain what changed.
Examples of better commit messages:
- Add login page
- Fix contact form validation
- Update homepage content
Step 7: Push Code to GitHub
After committing locally, send your changes to GitHub using:
git push origin main
Here:
- origin = remote repository name
- main = branch name
Creating a New Git Repository from Scratch
If your project is only on your computer and not yet connected to GitHub, follow these steps.
1. Go to your project folder
Open terminal inside your project folder.
2. Initialize Git
git init
This turns your folder into a Git repository.
3. Add all files
git add .
4. Make your first commit
git commit -m "Add initial files"
5. Connect your local project to GitHub
git remote add origin <repository-link>
Example:
git remote add origin https://github.com/username/projectname.git
6. Check branch name
git branch
7. Rename branch to main
git branch -M main
8. Push code to GitHub
git push -u origin main
Note: It is origin, not origan.
Understanding Branches
Branches let you work on features or fixes without affecting the main project immediately.
For example, if you want to create a new feature, you can create a branch for it. Once the feature is ready, you can merge it into the main branch.
See all branches
git branch
Switch to another branch
git checkout branch-name
Create and switch to a new branch
git checkout -b new-branch-name
Delete a branch
git branch -d branch-name
Merging Branches
After finishing work in a branch, you can merge it into your main branch.
Compare differences before merge
git diff branch-name
Merge a branch into your current branch
git merge branch-name
Example workflow:
- Create branch login-page
- Work on login feature
- Commit changes
- Switch to main
- Run git merge login-page
Pull Latest Changes from GitHub
If another developer updated the project, or if you updated code directly on GitHub, you should pull the latest changes.
git pull origin main
This downloads and merges the latest changes from GitHub into your local branch.
Delete Files or Folders in Git
If you want to remove a file or folder from the repository, use:
git rm file.txt
git rm -r folder-name
Then commit and push:
git commit -m "Remove unused files"
git push origin main
Undoing Changes
Beginners often make mistakes. That is normal. These commands help you undo common changes.
Discard changes in a file before staging
git checkout -- index.html
This restores the file to the last committed version.
Unstage a file after git add
git reset index.html
Undo last commit but keep changes
git reset --soft HEAD~1
Undo last commit and remove changes
git reset --hard HEAD~1
Warning: The hard reset command permanently removes changes that are not saved elsewhere.
Helpful Daily Git Workflow
Here is a simple daily routine for beginners:
- Open project folder
- Run git pull origin main
- Make your code changes
- Run git status
- Run git add .
- Run git commit -m "your message"
- Run git push origin main
Example Full Workflow
Here is a complete beginner example from start to finish:
git clone https://github.com/username/projectname.git
cd projectname
git status
git add .
git commit -m "Update homepage content"
git push origin main
How to Create a README.md File
A README.md file explains your project. It is usually the first file people read on GitHub.
Basic example:
# Project Name
## Description
Short details about the project.
## Features
- Login system
- Dashboard
- Contact form
## Installation
1. Clone the repository
2. Open the project
3. Run the project
## Author
Your name
Best Tips for Beginners
- run git status often
- write clear commit messages
- pull latest changes before starting work
- use branches for new features
- do not use git reset --hard unless you are sure
- practice with a test repository first
Common Mistakes Beginners Make
- forgetting to run git add before commit
- pushing to the wrong branch
- editing code without pulling latest updates
- using unclear commit messages like update files
- confusing Git with GitHub
Quick Command Summary
git --version
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
git config --list
git clone <repository-link>
git status
git add .
git add index.html
git commit -m "Your message"
git push origin main
git pull origin main
git init
git remote add origin <repository-link>
git branch
git branch -M main
git checkout branch-name
git checkout -b new-branch-name
git branch -d branch-name
git diff branch-name
git merge branch-name
git rm file.txt
git rm -r folder-name
git reset index.html
git reset --soft HEAD~1
git reset --hard HEAD~1
xd
How to clone private repositories to remote servers ie Github -> namecheap
1. In namecheap generate keys using below command
ssh-keygen -t ed25519 -C "server-deploy"
This creates two files: ~/.ssh/id_ed25519 , ~/.ssh/id_ed25519.pub
2. Copy the public key
cat ~/.ssh/id_ed25519.pub
3.Goto below directory and paste the public keys there
GitHub → Settings → SSH and GPG Keys → New SSH Key
4. Goto namecheap terminal and paste below command
git clone git@github.com:USERNAME/REPOSITORY.git #this will clone zip source code
to clone direct files use below commands
cd ~/public_html
git init
git remote add origin git@github.com:USERNAME/REPOSITORY.git
git pull origin main
5. Deploy updates from GitHub
git pull origin main
DOWNLOAD LATEST CODE FROM GITHUB TO LOCAL
git fetch origin
git reset --hard origin/main
Final Words
Git and GitHub may look technical in the beginning, but the basics are actually simple once you practice them. Start with cloning, checking status, adding files, committing, pulling, and pushing. After that, learn branches and merging. With regular use, these commands will become part of your normal coding workflow.