September 3, 20244 min readPublished on Hashnode

Git & GitHub: The Basics You Actually Need

Git & GitHub: The Basics You Actually Need

Why Git?

Imagine working on a project for weeks. You make hundreds of changes, add new features, fix bugs, and then suddenly something breaks.

Git acts like a version history for your project.

It tracks changes to your code and allows you to:

  • Go back to previous versions of your code.

  • Work on different features using branches.

  • Collaborate with other developers without overwriting their work.

  • Merge different features into the main project.

  • See exactly what changed and when.

  • Recover accidentally deleted or modified files.

GitHub builds on top of Git by providing a remote place to store repositories and collaborate with other developers.


Basic Git Commands

Configure Git

Set your name and email:

BASH
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Check your configuration:

BASH
git config --list

Create a Repository

Go to your project directory:

BASH
cd my-project

Initialize Git:

BASH
git init

Check the repository status:

BASH
git status

Add and Commit Changes

Add all changed files to the staging area:

BASH
git add .

Or add a specific file:

BASH
git add

Create a commit:

BASH
git commit -m "Add authentication"

Think of a commit as a snapshot of your project at that point in time.


Connect to GitHub

Add your GitHub repository as a remote:

BASH
git remote add origin https://github.com//.git

Check the remote:

BASH
git remote -v

Push your code to GitHub:

BASH
git push -u origin main

After setting the upstream branch, you can simply use:

BASH
git push

Clone a Repository

To download an existing repository from GitHub:

BASH
git clone

Clone a specific branch:

BASH
git clone --branch

Git Branches

Branches allow you to work on a feature or fix without directly modifying the main codebase.

For example:

CODE
main
 │
 ├── feature/auth
 │
 └── fix/login

Create and switch to a new branch:

BASH
git switch -c feature/auth

Switch to an existing branch:

BASH
git switch main

List branches:

BASH
git branch

List local and remote branches:

BASH
git branch -a

Delete a local branch:

BASH
git branch -d feature/auth

Delete a remote branch:

BASH
git push origin --delete feature/auth

Merge Branches

First, switch to the branch you want to merge into:

BASH
git switch main

Then merge the feature branch:

BASH
git merge feature/auth
CODE
feature/auth
     │
     │ git merge
     │  
     ▼
    main

Pull Changes from GitHub

Get the latest changes from the remote repository:

BASH
git pull origin main

git pull essentially combination of: git fetch + git merge

Fetch downloads the latest remote changes without merging them, while merge combines them with your local branch.

Safely Sync Local and Remote Changes:

Before pulling, check what changed on both sides:

BASH
git fetch origin

Check commits coming from remote:

BASH
git log HEAD..origin/main --oneline

Check your local commits not pushed yet:

BASH
git log origin/main..HEAD --oneline

Check file differences:

BASH
git diff HEAD origin/main

Check for uncommitted changes:

BASH
git status

If you have local changes you want to protect:

BASH
git stash push -u -m "pre-sync-backup"
git branch backup-before-sync

Then choose how to sync.

Keep your local commits and apply them on top of remote:

BASH
git pull --rebase origin main

Restore your stashed changes if needed:

BASH
git stash pop

Discard local differences and match the remote:

BASH
git reset --hard origin/main

Use reset --hard carefully - it discards local tracked changes. Creating a backup branch first gives you a way to recover your previous committed state.


Undo Changes

Undo the last commit but keep the changes

BASH
git reset --soft HEAD~1

Undo the last commit and discard the changes

BASH
git reset --hard HEAD~1

Be careful with --hard because it can permanently discard local changes.

Undo a commit safely

Instead of removing a commit from history, create a new commit that reverses it:

BASH
git revert

Modify the last commit

BASH
git commit --amend

Restore Deleted Files

If a file was deleted but the deletion hasn't been committed:

BASH
git restore

To restore a file from an older commit:

BASH
git restore --source= --

View the history of a specific file:

BASH
git log --

Forking a Repository

A fork is a copy of someone else's GitHub repository under your own GitHub account.

It's commonly used when contributing to open-source projects where you don't have direct write access.

The usual workflow is:

CODE
Original Repository
        ↓
       Fork
        ↓
Your GitHub Repository
        ↓
      Clone
        ↓
   Make Changes
        ↓
      Push
        ↓
   Pull Request
        ↓
Original Repository

Clone your fork:

BASH
git clone https://github.com//.git

The Git Commands I Use Most

If you don't want to memorize everything, start with these:

BASH
git status
git add .
git commit -m "message"
git push
git pull
git switch -c 
git switch 
git merge 
git log --oneline
git clone

These commands cover most of the day-to-day Git workflow.

Git is basically the tool that manages your project's history; GitHub is where you can store that history online and collaborate with others.