GitHub Fundamentals
What is GitHub?
GitHub hosts Git repositories online and adds collaboration features such as pull requests, issues, reviews,
discussions, permissions, releases, and automation.
Repository
A GitHub repository contains your project files, Git history, branches, issues, pull requests, and other
project information.
Create a repository
A repository can be created directly on GitHub and then cloned locally.
Clone vs Fork
- Clone
- Copies a repository from a remote location onto your computer.
- Fork
- Creates your own GitHub-hosted copy of another repository.
A common open-source workflow is:
fork → clone → branch → commit → push → pull request.
Pull Request
A pull request proposes merging changes from one branch into another branch, usually after
review.
Typical pull request flow
- Create a feature branch.
- Make changes.
- Commit the changes.
- Push the branch to GitHub.
- Open a pull request.
- Review and discuss the changes.
- Merge the pull request.
Issues
GitHub Issues can be used to track bugs, tasks, feature requests, improvements, and project discussions.
Repository Basics
git init Initialize a Repository
Creates a new Git repository in the current directory.
git init
Typical usage
mkdir my-project
cd my-project
git init
git clone Clone a Repository
Creates a local copy of an existing repository.
git clone https://github.com/user/project.git
Clone with a custom directory name
git clone https://github.com/user/project.git my-project
git status Check Repository State
git status
This is one of the most useful Git commands. Run it frequently to understand what Git currently sees.
git log View Commit History
git log
Compact history
git log --oneline
Graph view
git log --oneline --graph --decorate --all
git diff Compare Changes
Shows changes that have not yet been staged.
git diff
View staged changes
git diff --staged
Undoing Changes
git restore Discard Working Directory Changes
Restores a file to the version recorded by Git and discards unstaged changes to that file.
git restore file.txt
This can permanently discard local changes, so use it carefully.
Unstage a File
Remove a file from the staging area while keeping its working directory changes.
git restore --staged file.txt
git revert Safely Undo a Commit
Creates a new commit that reverses the effect of an earlier commit. This is often appropriate for commits
that have already been shared.
git revert <commit-id>
git reset Move HEAD
git reset changes where your branch points and can also modify the staging area or working
directory depending on the mode.
Unstage the latest commit but keep changes
git reset --soft HEAD~1
Mixed reset
git reset HEAD~1
Reset can rewrite local history. Be cautious when using it on commits that have already been pushed.
Common Mistakes
Committing Everything Without Checking
Avoid blindly running git add . without checking what changed.
git status
git diff
git add .
git status
git commit -m "Meaningful message"
Committing Secrets
Never commit passwords, API keys, private keys, tokens, or other sensitive credentials.
.env
*.pem
secrets.json
Working Directly on Main
For collaborative development, using short-lived feature branches often makes review and integration safer.
Using Force Push Carelessly
Force-pushing can rewrite remote history and disrupt other contributors.
git push --force
When a force push is necessary, understand exactly which history you are replacing and follow your team's
workflow.