Git and GitHub: What They Are, Why They Matter, and How to Use Them
If you are learning coding, web development, data science, or software engineering, you will keep seeing Git and GitHub. They are closely connected, but they are not the same thing. Git is the system that tracks changes in your project. GitHub is the online platform that hosts Git repositories and adds collaboration tools around them.
What is Git?
Git is a free, open-source distributed version control system. In simple terms, it records changes to your files over time, so you can see what changed, return to an earlier version, and work more safely without overwriting important files. Because Git is distributed, each developer can keep a full copy of the project history on their own machine.
Why use Git?
Git gives your work structure and safety. Instead of creating messy file names like final-v2-final-really-final, you save meaningful snapshots called commits. You can also create branches, which let you work on new features or experiments without affecting the main version of the project.
In practice, Git helps you:
- track project history clearly
- recover from mistakes
- test ideas safely in branches
- collaborate without overwriting others’ work
- build a more professional workflow
What is GitHub?
GitHub is a cloud-based platform built around Git repositories. It lets you store projects online, work with other people, review code changes, manage branches, and use features like pull requests and automation.
Why use GitHub?
GitHub becomes valuable when your project needs an online home. It gives you backup, collaboration, visibility, and workflow tools on top of Git. It is useful for working across multiple devices, sharing code with a team, reviewing changes, and building a public or private portfolio of projects.
Git vs GitHub
The easiest way to remember the difference is this:
- Git manages version history.
- GitHub hosts Git repositories online and supports collaboration.
You can use Git without GitHub on your own machine. But most modern developers use Git with GitHub because GitHub makes sharing, reviewing, and managing projects much easier.
Comparison table
| Area | Git | GitHub |
|---|---|---|
| What it is | Version control system | Hosting and collaboration platform |
| Main purpose | Track changes in files and project history | Store repositories online and support teamwork |
| Works locally | Yes | Mainly remote and web-based |
| Branches and commits | Core built-in features | Shows and manages them online |
| Collaboration | Limited by itself | Strong collaboration with pull requests and review |
| Automation | Not a hosting or automation platform | Supports automation and workflow tools |
| Best for | Local version control | Remote hosting, sharing, and team workflow |
How they work together
A normal workflow is simple. You create or clone a repository with Git, edit files locally, commit the changes, and then push them to GitHub. Git handles the version history, while GitHub handles the remote hosting and collaboration layer.
Start here
Download Git here: Git Install
Start GitHub here: GitHub Get Started
Git Commands Cheat Sheet and Reference Guide: 100 Commands Under Proper Categories
1) Setup and configuration
git --version
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --list
git config --global init.defaultBranch main
git help commit
git commit --help
git config user.name
git config --global core.autocrlf true
git config --global core.editor "code --wait"
2) Starting and cloning repositories
git init
git init --bare
git clone https://github.com/username/repo.git
git clone https://github.com/username/repo.git my-folder
git clone --branch dev https://github.com/username/repo.git
git clone --depth 1 https://github.com/username/repo.git
git status
git status -s
git rev-parse --show-toplevel
git rev-parse --is-inside-work-tree
3) Tracking files and committing
git add filename
git add .
git add -A
git add -p
git commit -m "Your message"
git commit -am "Your message"
git commit --amend
git commit --amend --no-edit
git log
git log --oneline --graph --all
4) Inspecting changes
git diff
git diff --cached
git diff commit1 commit2
git diff main..dev
git diff --name-only
git blame filename
git show HEAD
git log -S "search text"
git log --grep="fix"
git ls-files
5) Branching basics
git branch
git branch -a
git branch dev
git switch dev
git switch -c dev
git checkout dev
git checkout -b dev
git branch -m new-name
git branch -d dev
git branch -D dev
6) Working with remotes
git remote -v
git remote add origin https://github.com/username/repo.git
git remote remove origin
git remote rename origin old-origin
git remote set-url origin https://github.com/username/new-repo.git
git remote show origin
git fetch origin
git fetch --prune
git pull origin main
git pull --rebase origin main
7) Pushing to same or different branches
git push
git push origin main
git push -u origin dev
git push origin HEAD:staging
git push origin main:dev
git push --force
git push --force-with-lease
git push --all origin
git push --tags
git push origin --delete dev
8) Merging, rebasing, integrating
git merge dev
git merge --no-ff dev
git merge --abort
git rebase main
git rebase origin/main
git rebase --continue
git rebase --abort
git rebase -i HEAD~5
git cherry-pick <commit-hash>
git revert <commit-hash>
9) Undoing, restoring, cleaning
git restore --staged filename
git restore filename
git restore --source=<commit-hash> filename
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git clean -f
git clean -fd
git clean -fdn
git reflog
10) Stash, tags, advanced everyday use
git stash
git stash push -m "work in progress"
git stash list
git stash apply
git stash pop
git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
git tag
git bisect start
git archive --format=zip --output=project.zip HEAD
Git Commands Reference Guide: 100 Essential Commands in Tables by Category
1) Setup and configuration
| Command | What it does | When you use it |
|---|---|---|
git --version | Shows the installed Git version on your computer. | Use it first to confirm Git is installed properly. |
git config --global user.name "Your Name" | Sets the name Git will attach to your commits across all repositories on your machine. | Use it during first-time Git setup. |
git config --global user.email "[email protected]" | Sets the email Git will attach to your commits across all repositories. | Use it during initial setup or when switching identity. |
git config --list | Displays all Git configuration values currently active on your system. | Use it to verify your settings or troubleshoot config issues. |
git config --global init.defaultBranch main | Changes the default branch name for newly created repositories to main. | Use it if you want modern branch naming by default. |
git help commit | Opens the detailed manual page for the commit command. | Use it when you want full documentation for a command. |
git commit --help | Another way to open the help page for git commit. | Use it when you need syntax or options quickly. |
git config user.name | Shows the current username configured in Git. | Use it to confirm which identity Git is using. |
git config --global core.autocrlf true | Helps Git handle Windows line endings more smoothly when working across different operating systems. | Use it mainly on Windows systems. |
git config --global core.editor "code --wait" | Sets VS Code as the default editor Git should open for commit messages, rebases, and merges. | Use it if you work in VS Code and want Git to open it automatically. |
2) Starting and cloning repositories
| Command | What it does | When you use it |
|---|---|---|
git init | Creates a brand-new Git repository in the current folder. | Use it when starting version control for a new project. |
git init --bare | Creates a bare repository without working files, usually used as a remote/shared repository. | Use it for server-side or central repository setups. |
git clone https://github.com/username/repo.git | Downloads a copy of a remote repository to your machine. | Use it when you want to start working on an existing repo. |
git clone https://github.com/username/repo.git my-folder | Clones a repository but places it inside a folder name you choose. | Use it when you want a custom local folder name. |
git clone --branch dev https://github.com/username/repo.git | Clones the repo and checks out a specific branch immediately. | Use it when you want to work directly from a non-main branch. |
git clone --depth 1 https://github.com/username/repo.git | Performs a shallow clone with only the latest commit history. | Use it when you want a faster clone and do not need full history. |
git status | Shows the current state of the repo, including changed, staged, and untracked files. | Use it constantly while working in Git. |
git status -s | Shows a shorter and more compact version of repository status. | Use it when you want a quicker, cleaner view. |
git rev-parse --show-toplevel | Displays the root directory of the current Git repository. | Use it when you are unsure where the repo starts. |
git rev-parse --is-inside-work-tree | Tells you whether the current folder is inside a Git working repository. | Use it when troubleshooting or checking your location in terminal. |
3) Tracking files and committing
| Command | What it does | When you use it |
|---|---|---|
git add filename | Stages one specific file so it will be included in the next commit. | Use it when you want precise control over what gets committed. |
git add . | Stages all changed files in the current directory and below. | Use it for quick commits when you want all current changes included. |
git add -A | Stages all changes in the entire repository, including deletions. | Use it when you want everything tracked in one go. |
git add -p | Lets you stage selected chunks of a file instead of the whole file. | Use it when only part of a file is ready to commit. |
git commit -m "Your message" | Creates a commit from staged changes with a message. | Use it to save a clear snapshot of completed work. |
git commit -am "Your message" | Commits changes to already tracked files without running git add separately. | Use it for quick updates to files Git already knows about. |
git commit --amend | Reopens and modifies the most recent commit. | Use it when you forgot a file or want to change the last commit message. |
git commit --amend --no-edit | Adds new staged changes into the last commit without changing its message. | Use it when the message is fine but the content needs fixing. |
git log | Shows the full commit history of the current branch. | Use it when reviewing previous work or commit sequence. |
git log --oneline --graph --all | Shows a compact visual history of commits and branches. | Use it to understand branch structure and recent activity more easily. |
4) Inspecting changes
| Command | What it does | When you use it |
|---|---|---|
git diff | Shows changes you made that are not yet staged. | Use it before staging to review your edits. |
git diff --cached | Shows staged changes that will go into the next commit. | Use it before committing to double-check staged content. |
git diff commit1 commit2 | Compares the content differences between two commits. | Use it when reviewing what changed between two points in history. |
git diff main..dev | Compares one branch against another. | Use it to see how dev differs from main. |
git diff --name-only | Lists only the file names that changed. | Use it when you only need a quick summary of changed files. |
git blame filename | Shows who last changed each line in a file and in which commit. | Use it when investigating the origin of a line of code. |
git show HEAD | Displays detailed information about the latest commit, including changed files and diff. | Use it when reviewing the most recent commit. |
git log -S "search text" | Finds commits where a particular text string was added or removed. | Use it when tracking down when a piece of code appeared or changed. |
git log --grep="fix" | Searches commit messages for a specific word or phrase. | Use it when looking for commits by purpose or message keyword. |
git ls-files | Lists all files currently tracked by Git. | Use it when you want to see exactly what Git is managing. |
5) Branching basics
| Command | What it does | When you use it |
|---|---|---|
git branch | Lists your local branches and shows which one you are on. | Use it to check your current working branch. |
git branch -a | Lists both local and remote-tracking branches. | Use it when you want to see all available branches. |
git branch dev | Creates a new branch called dev but does not switch to it. | Use it when you want to create a branch first and switch later. |
git switch dev | Switches to an existing branch. | Use it for day-to-day movement between branches. |
git switch -c dev | Creates a new branch and switches to it immediately. | Use it when starting a new feature or task branch. |
git checkout dev | Older command for switching to an existing branch. | Use it when reading older tutorials or working in environments that still use checkout. |
git checkout -b dev | Older command that creates and switches to a new branch. | Use it the same way as git switch -c dev. |
git branch -m new-name | Renames your current branch. | Use it when you want to clean up or standardize branch naming. |
git branch -d dev | Deletes a local branch that has already been merged safely. | Use it for branch cleanup after finishing work. |
git branch -D dev | Force deletes a local branch even if it has not been merged. | Use it carefully when you are sure you no longer need that branch. |
6) Working with remotes
| Command | What it does | When you use it |
|---|---|---|
git remote -v | Shows the remote repositories connected to your local repo, including fetch and push URLs. | Use it to confirm where your repo is linked. |
git remote add origin https://github.com/username/repo.git | Adds a remote called origin pointing to a repository URL. | Use it when connecting a local repo to GitHub. |
git remote remove origin | Removes the remote called origin from the current local repository. | Use it when you want to disconnect from the current remote repo. |
git remote rename origin old-origin | Renames an existing remote. | Use it when keeping multiple remotes and wanting clearer names. |
git remote set-url origin https://github.com/username/new-repo.git | Changes the URL of an existing remote without removing it first. | Use it when moving the project to a new GitHub repo. |
git remote show origin | Shows detailed information about a remote, including tracked branches. | Use it when checking remote branch relationships. |
git fetch origin | Downloads the latest data from the remote without merging it into your branch. | Use it when you want updates but not automatic changes to your work. |
git fetch --prune | Fetches updates and also removes references to remote branches that no longer exist. | Use it to keep remote branch listings clean. |
git pull origin main | Fetches changes from remote main and merges them into your current branch. | Use it when bringing the latest remote work into your branch. |
git pull --rebase origin main | Fetches changes and rebases your local commits on top of the updated remote branch. | Use it when you want a cleaner, more linear history. |
7) Pushing to same or different branches
| Command | What it does | When you use it |
|---|---|---|
git push | Pushes your current branch to the remote branch it is already tracking. | Use it after upstream has already been set once. |
git push origin main | Pushes your local main branch to the remote main branch. | Use it when you want to push explicitly to main. |
git push -u origin dev | Pushes your local dev branch and sets it to track the remote dev branch. | Use it the first time you push a new branch. |
git push origin HEAD:staging | Pushes your current local branch to a remote branch named staging. | Use it when you want to avoid pushing to main and target a different branch. |
git push origin main:dev | Pushes your local main branch into the remote dev branch. | Use it when local and remote branch names are intentionally different. |
git push --force | Force pushes and overwrites the remote branch history. | Use it only when you fully understand the risk, especially after rebasing. |
git push --force-with-lease | Force pushes more safely by first checking whether the remote changed unexpectedly. | Use it instead of plain --force whenever possible. |
git push --all origin | Pushes all local branches to the remote. | Use it when migrating or backing up a repository. |
git push --tags | Pushes all local tags to the remote. | Use it when publishing releases or version tags. |
git push origin --delete dev | Deletes the dev branch from the remote repository. | Use it when cleaning up remote branches after a merge. |
8) Merging, rebasing, and integrating work
| Command | What it does | When you use it |
|---|---|---|
git merge dev | Merges the dev branch into your current branch. | Use it when combining completed work from one branch into another. |
git merge --no-ff dev | Merges with an explicit merge commit, even if Git could fast-forward. | Use it when you want branch history to stay visibly separate. |
git merge --abort | Cancels an in-progress merge and returns the repo to its previous state. | Use it if a merge goes wrong or produces unwanted conflicts. |
git rebase main | Moves your current branch commits so they sit on top of main. | Use it to update your branch with the latest main changes cleanly. |
git rebase origin/main | Rebases your work on top of the latest remote main. | Use it when you want to align your branch with the newest remote state. |
git rebase --continue | Continues a rebase after you have fixed conflicts. | Use it during conflict resolution in a rebase workflow. |
git rebase --abort | Stops a rebase and returns to the state before it started. | Use it if the rebase becomes too messy or incorrect. |
git rebase -i HEAD~5 | Opens an interactive rebase for the last five commits so you can edit, reorder, squash, or remove them. | Use it when cleaning commit history before sharing or merging. |
git cherry-pick <commit-hash> | Applies one specific commit from another branch onto your current branch. | Use it when you want one change but not the whole branch. |
git revert <commit-hash> | Creates a new commit that reverses an earlier commit. | Use it to safely undo shared commits without rewriting history. |
9) Undoing, restoring, and cleaning
| Command | What it does | When you use it |
|---|---|---|
git restore --staged filename | Removes a file from the staging area but keeps your edits in the working directory. | Use it when you staged a file by mistake. |
git restore filename | Discards local unstaged changes in a file and restores it to the last committed version. | Use it when you want to throw away unwanted edits. |
git restore --source=<commit-hash> filename | Replaces a file with the version from a specific commit. | Use it when you want to recover an earlier version of a file. |
git reset --soft HEAD~1 | Moves the branch back one commit while keeping all changes staged. | Use it when you want to redo the last commit neatly. |
git reset --mixed HEAD~1 | Moves back one commit and keeps the changes locally, but unstaged. | Use it when you want to recommit differently. |
git reset --hard HEAD~1 | Moves back one commit and deletes the local changes completely. | Use it only when you are sure you want to discard that work. |
git clean -f | Deletes untracked files from the working directory. | Use it to remove clutter that Git is not tracking. |
git clean -fd | Deletes untracked files and untracked directories. | Use it for a deeper clean of your working folder. |
git clean -fdn | Shows what git clean -fd would remove, without actually removing it. | Use it as a safe preview before deleting files. |
git reflog | Shows the recent movement of HEAD, including commits, resets, rebases, and branch switches. | Use it to recover lost commits or undo serious mistakes. |
10) Stash, tags, and advanced everyday use
| Command | What it does | When you use it |
|---|---|---|
git stash | Temporarily saves your uncommitted changes and clears the working directory. | Use it when you need to switch branches without committing unfinished work. |
git stash push -m "work in progress" | Saves your current changes into the stash with a label or description. | Use it when you want multiple stashes and need to recognize them later. |
git stash list | Shows all saved stashes. | Use it when you want to see what temporary work has been stored. |
git stash apply | Restores the most recent stash but keeps it in the stash list. | Use it when you may want to reuse that stash again. |
git stash pop | Restores the most recent stash and removes it from the stash list. | Use it when you are done storing that temporary work. |
git tag v1.0.0 | Creates a lightweight tag pointing to the current commit. | Use it for simple version marking. |
git tag -a v1.0.0 -m "Version 1.0.0" | Creates an annotated tag with extra metadata and a message. | Use it for proper release tagging. |
git tag | Lists all tags in the repository. | Use it when checking existing versions or release markers. |
git bisect start | Starts the bisect process to help find which commit introduced a bug. | Use it when debugging a problem that appeared somewhere in history. |
git archive --format=zip --output=project.zip HEAD | Creates a clean ZIP archive of the current committed project without Git metadata. | Use it when you want to share or deploy a snapshot of the project. |
Most useful commands to master first
| Command | What it does | Why it matters first |
|---|---|---|
git clone <repo-url> | Copies a remote repo to your machine. | This is how most work begins. |
git status | Shows the current repo state. | It tells you what is happening right now. |
git add . | Stages your changes. | It prepares work for committing. |
git commit -m "message" | Saves a snapshot of your changes. | This is the core Git workflow. |
git branch | Lists branches. | Helps you understand where you are working. |
git switch -c dev | Creates and moves to a new branch. | Essential for safe feature work. |
git pull origin main | Gets the latest changes from remote. | Keeps your repo up to date. |
git push -u origin dev | Pushes a new branch and links it. | Needed when sharing new work. |
git remote -v | Shows connected remotes. | Helps confirm where code will push. |
git remote remove origin | Disconnects the current repo link. | Important when moving code to another repo. |
git remote add origin <new-url> | Connects to a different remote repo. | Essential for repo migration or copying projects. |
git merge dev | Combines one branch into another. | Needed when finishing branch work. |
git restore --staged filename | Unstages a file. | Useful when you add the wrong file. |
git reset --soft HEAD~1 | Undoes the last commit but keeps changes. | Very useful for fixing recent mistakes. |
git stash | Stores unfinished work temporarily. | Helps when switching tasks quickly. |
git log --oneline --graph --all | Shows commit history clearly. | Helps you understand project history visually. |
git reflog | Shows recent HEAD movements. | Critical for recovering from mistakes. |
