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

AreaGitGitHub
What it isVersion control systemHosting and collaboration platform
Main purposeTrack changes in files and project historyStore repositories online and support teamwork
Works locallyYesMainly remote and web-based
Branches and commitsCore built-in featuresShows and manages them online
CollaborationLimited by itselfStrong collaboration with pull requests and review
AutomationNot a hosting or automation platformSupports automation and workflow tools
Best forLocal version controlRemote 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

CommandWhat it doesWhen you use it
git --versionShows 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 --listDisplays all Git configuration values currently active on your system.Use it to verify your settings or troubleshoot config issues.
git config --global init.defaultBranch mainChanges the default branch name for newly created repositories to main.Use it if you want modern branch naming by default.
git help commitOpens the detailed manual page for the commit command.Use it when you want full documentation for a command.
git commit --helpAnother way to open the help page for git commit.Use it when you need syntax or options quickly.
git config user.nameShows the current username configured in Git.Use it to confirm which identity Git is using.
git config --global core.autocrlf trueHelps 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

CommandWhat it doesWhen you use it
git initCreates a brand-new Git repository in the current folder.Use it when starting version control for a new project.
git init --bareCreates 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.gitDownloads 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-folderClones 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.gitClones 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.gitPerforms a shallow clone with only the latest commit history.Use it when you want a faster clone and do not need full history.
git statusShows the current state of the repo, including changed, staged, and untracked files.Use it constantly while working in Git.
git status -sShows a shorter and more compact version of repository status.Use it when you want a quicker, cleaner view.
git rev-parse --show-toplevelDisplays the root directory of the current Git repository.Use it when you are unsure where the repo starts.
git rev-parse --is-inside-work-treeTells 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

CommandWhat it doesWhen you use it
git add filenameStages 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 -AStages all changes in the entire repository, including deletions.Use it when you want everything tracked in one go.
git add -pLets 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 --amendReopens 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-editAdds new staged changes into the last commit without changing its message.Use it when the message is fine but the content needs fixing.
git logShows the full commit history of the current branch.Use it when reviewing previous work or commit sequence.
git log --oneline --graph --allShows a compact visual history of commits and branches.Use it to understand branch structure and recent activity more easily.

4) Inspecting changes

CommandWhat it doesWhen you use it
git diffShows changes you made that are not yet staged.Use it before staging to review your edits.
git diff --cachedShows staged changes that will go into the next commit.Use it before committing to double-check staged content.
git diff commit1 commit2Compares the content differences between two commits.Use it when reviewing what changed between two points in history.
git diff main..devCompares one branch against another.Use it to see how dev differs from main.
git diff --name-onlyLists only the file names that changed.Use it when you only need a quick summary of changed files.
git blame filenameShows 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 HEADDisplays 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-filesLists all files currently tracked by Git.Use it when you want to see exactly what Git is managing.

5) Branching basics

CommandWhat it doesWhen you use it
git branchLists your local branches and shows which one you are on.Use it to check your current working branch.
git branch -aLists both local and remote-tracking branches.Use it when you want to see all available branches.
git branch devCreates 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 devSwitches to an existing branch.Use it for day-to-day movement between branches.
git switch -c devCreates a new branch and switches to it immediately.Use it when starting a new feature or task branch.
git checkout devOlder command for switching to an existing branch.Use it when reading older tutorials or working in environments that still use checkout.
git checkout -b devOlder command that creates and switches to a new branch.Use it the same way as git switch -c dev.
git branch -m new-nameRenames your current branch.Use it when you want to clean up or standardize branch naming.
git branch -d devDeletes a local branch that has already been merged safely.Use it for branch cleanup after finishing work.
git branch -D devForce 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

CommandWhat it doesWhen you use it
git remote -vShows 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.gitAdds a remote called origin pointing to a repository URL.Use it when connecting a local repo to GitHub.
git remote remove originRemoves 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-originRenames an existing remote.Use it when keeping multiple remotes and wanting clearer names.
git remote set-url origin https://github.com/username/new-repo.gitChanges the URL of an existing remote without removing it first.Use it when moving the project to a new GitHub repo.
git remote show originShows detailed information about a remote, including tracked branches.Use it when checking remote branch relationships.
git fetch originDownloads 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 --pruneFetches updates and also removes references to remote branches that no longer exist.Use it to keep remote branch listings clean.
git pull origin mainFetches 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 mainFetches 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

CommandWhat it doesWhen you use it
git pushPushes your current branch to the remote branch it is already tracking.Use it after upstream has already been set once.
git push origin mainPushes your local main branch to the remote main branch.Use it when you want to push explicitly to main.
git push -u origin devPushes 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:stagingPushes 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:devPushes your local main branch into the remote dev branch.Use it when local and remote branch names are intentionally different.
git push --forceForce pushes and overwrites the remote branch history.Use it only when you fully understand the risk, especially after rebasing.
git push --force-with-leaseForce pushes more safely by first checking whether the remote changed unexpectedly.Use it instead of plain --force whenever possible.
git push --all originPushes all local branches to the remote.Use it when migrating or backing up a repository.
git push --tagsPushes all local tags to the remote.Use it when publishing releases or version tags.
git push origin --delete devDeletes the dev branch from the remote repository.Use it when cleaning up remote branches after a merge.

8) Merging, rebasing, and integrating work

CommandWhat it doesWhen you use it
git merge devMerges the dev branch into your current branch.Use it when combining completed work from one branch into another.
git merge --no-ff devMerges with an explicit merge commit, even if Git could fast-forward.Use it when you want branch history to stay visibly separate.
git merge --abortCancels 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 mainMoves 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/mainRebases 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 --continueContinues a rebase after you have fixed conflicts.Use it during conflict resolution in a rebase workflow.
git rebase --abortStops a rebase and returns to the state before it started.Use it if the rebase becomes too messy or incorrect.
git rebase -i HEAD~5Opens 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

CommandWhat it doesWhen you use it
git restore --staged filenameRemoves 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 filenameDiscards 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> filenameReplaces 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~1Moves 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~1Moves back one commit and keeps the changes locally, but unstaged.Use it when you want to recommit differently.
git reset --hard HEAD~1Moves back one commit and deletes the local changes completely.Use it only when you are sure you want to discard that work.
git clean -fDeletes untracked files from the working directory.Use it to remove clutter that Git is not tracking.
git clean -fdDeletes untracked files and untracked directories.Use it for a deeper clean of your working folder.
git clean -fdnShows what git clean -fd would remove, without actually removing it.Use it as a safe preview before deleting files.
git reflogShows 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

CommandWhat it doesWhen you use it
git stashTemporarily 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 listShows all saved stashes.Use it when you want to see what temporary work has been stored.
git stash applyRestores the most recent stash but keeps it in the stash list.Use it when you may want to reuse that stash again.
git stash popRestores 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.0Creates 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 tagLists all tags in the repository.Use it when checking existing versions or release markers.
git bisect startStarts 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 HEADCreates 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

CommandWhat it doesWhy it matters first
git clone <repo-url>Copies a remote repo to your machine.This is how most work begins.
git statusShows 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 branchLists branches.Helps you understand where you are working.
git switch -c devCreates and moves to a new branch.Essential for safe feature work.
git pull origin mainGets the latest changes from remote.Keeps your repo up to date.
git push -u origin devPushes a new branch and links it.Needed when sharing new work.
git remote -vShows connected remotes.Helps confirm where code will push.
git remote remove originDisconnects 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 devCombines one branch into another.Needed when finishing branch work.
git restore --staged filenameUnstages a file.Useful when you add the wrong file.
git reset --soft HEAD~1Undoes the last commit but keeps changes.Very useful for fixing recent mistakes.
git stashStores unfinished work temporarily.Helps when switching tasks quickly.
git log --oneline --graph --allShows commit history clearly.Helps you understand project history visually.
git reflogShows recent HEAD movements.Critical for recovering from mistakes.