Skip to content

Latest commit

 

History

History
469 lines (338 loc) · 11.2 KB

File metadata and controls

469 lines (338 loc) · 11.2 KB

🚀 Git & GitHub – 0 to Pro Reference

A practical cheat‑sheet for mastering Git and GitHub from basics to advanced workflows.

Table of contents

🖥️ Command Line Basics

ls                       # List files and folders
cd ~/Desktop/folder     # Change directory

⚠️ Run git commands inside the project folder.


🧠 Git Concepts

  • Version = Commit
  • Version History = Commit history
  • Working Area → Staging Area → Commit History

🆕 Initialize Git

git init  # Git will start tracking all changes in the current folder
git status  # Show all changes since the previous commit

  • Working Area = contains changes start in the working area
  • Staging Area = contains changes that will go into the next commit

📦 Staging Changes

git add file  # Pick individual file
git add folder/  # Pick all files inside a folder (and subfolders)
git add .  # Pick all files (in folder command line is running in)

💾 Creating Commits

git commit -m "message"  # Creates a commit with a message attached
git commit -m "message" --amend   # Update previous commit instead of creating new one
git log  # View the commit history
git log --all  # Show all commits (not just current branch)
git log --all --graph  # Show branching visually in the command line
git log --oneline

(back to top)


Conventional Commits

  • Structure:
<type>: <description>

[optional body]

[optional footer]
  • Example:
git commit -m "feat: advanced login" `
-m "Added JWT token verification middleware" `
-m "Fixed bug in session timeout logic" `
-m "Closes #42"

Specify the type of commit:

  • feat: The new feature you're adding to a particular application
  • fix: A bug fix
  • style: Feature and updates related to styling
  • refactor: Refactoring a specific section of the codebase
  • test: Everything related to testing
  • docs: Everything related to documentation
  • chore: Regular code maintenance.[ You can also use emojis to represent commit types]

(back to top)


⚙️ Configure Git

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

🔄 Reset & Checkout

Staging → Working

git reset file
git reset folder/
git reset .

git reset --hard head~1  #to completely go back to previous commit | Deletes the latest commit from history.
git push -f   # to remove the future commit from present commit | Forces remote to forget that commit too

Working → Remove Changes

git checkout <commit>  <file/folder>  #syntax
git checkout -- file
git checkout -- folder/
git checkout -- .

(back to top)


🕒 Viewing Previous Commits

git checkout <commit_hash>
git checkout <branch_name>
  • HEAD → current commit
  • master/main → latest commit on branch

♻️ Restore Files from Previous Commit

git checkout <hash> file
git checkout <hash> folder/
git checkout <hash> .

🧩 Git Aliases

git config --global alias.s "status"
git s
git config --global --unset alias.s # to remove an alias

🙈 .gitignore

Tell Git which files/folders NOT to track. .gitignore


Delete Git

rm -rf .git # to remove git completely from the project - it deletes the .git folder

Windows PowerShell:

Remove-Item -Recurse -Force -LiteralPath ".\.git"
  • Delete the .git from hidden folders in the project

(back to top)


☁️ GitHub Basics

  • Local Repo = On your computer
  • Remote Repo = On GitHub

🔗 Connect to GitHub

git remote add <remote_name> <url> # Link a local repository to a remote repository andgive a name for this link
git remote  # List all remote repositories that are linked
git remote -v # List all remote repositories (but with more detail)
git remote remove origin  # Removes the link to the remote repository named "origin"
git config --global credential.username <username>  # Configure your GitHub username so you can get access to your Github repository

⬆️ Push Code

git push origin main  # Upload a branch of your git version history to your remote repository
git push origin main --set-upstream # Next time you are on the main branch and you run git push, it will automatically push the main branch to origin
git push origin main -f # Force-push the branch to the remote repository (it will overwrite what's on the remote repository)

⬇️ Clone / Pull

git clone <url> <folder_name>

git fetch
# The git fetch command is primarily used to safely download the latest commits, branches, and tags from a remote repository to your local machine without modifying your local working files

git pull origin main
git pull origin main --set-upstream

(back to top)


🌿 Branching

git branch feature1
git checkout feature1
git branch -D feature1
git push origin --delete project/meme-page
  • HEAD -> feature1 → current branch

🔀 Merging

git checkout main
git merge feature1 -m "message"  
git merge --no-ff feature1 -m "message" # no fast forward commit, feature 1 is shown as separate and merged

⚠️ Merge Conflicts

When conflicts occur, Git marks the file like this:

<<<<<<< HEAD
code from current branch
=======
code from merging branch
>>>>>>> branch

To resolve:

  1. Remove conflict markers
  2. Keep the final code you want
  3. Commit the changes
git add .
git commit -m "resolved conflict"

🧪 Feature Branch Workflow

git branch new-feature
git checkout new-feature
git add .
git commit -m "new feature"
git push origin new-feature

Then create a Pull Request on GitHub and merge into main.

(back to top)


🔄 Sync After Merge

git checkout main
git pull origin main

Delete branch

git branch -D branch-name

Git Rebase

Rebasing is the process of moving or combining a sequence of commits to a new base commit. It is primarily used to maintain a clean, linear project history.

Use this to edit, squash, or reorder commits before they are shared.

git rebase -i HEAD~n
  • Common Commands in Interactive Mode:
    • reword: Use the commit, but edit the commit message.
    • squash: Meld the commit into the previous one.
    • pick: Use the commit as is.
    • drop: Remove the commit entirely.

Git Reflog

Think of Reflog as your local "undo" history. While git log shows the history of the branch, git reflog records every time your HEAD pointer moves—including resets, checkouts, and rebases.

When to Use:

  • You accidentally deleted a branch.
  • You performed a git reset --hard and lost work.
  • A rebase went wrong and you want to go back.

How to Undo a Rebase

  1. Find the state before the rebase:
git reflog
  1. Locate the entry that says rebase (start) and look at the line directly below it (e.g., HEAD@{5}).

  2. Reset back to that moment:

git reset --hard HEAD@{n}

Git Revert

If the merge commit has been pushed to a shared or remote repository (like GitHub, GitLab, etc.), you should use git revert. This approach is safer for collaboration because it creates a new commit that undoes the changes of the merge, rather than rewriting the shared history.

  • Identify the merge commit; Use git log --oneline --graph to find the hash (ID) of the specific merge commit you want to undo.
  • Revert the merge commit: Run the git revert command with the -m (mainline parent number) option:
git revert -m 1 <merge-commit-hash>

The -m 1 flag tells Git to keep the parent side of the merge that was the branch you merged into (e.g., main or master branch).

  • Resolve conflicts (if any): Git may pause and ask you to resolve conflicts if subsequent changes conflict with the revert. After resolving, use git add and then git commit to finalize the revert.

  • Push the new revert commit: Push the new commit to the remote repository:

git push origin <your-branch-name>

📘 Git: Revert vs Reflog vs Rebase

Feature git revert git reflog git rebase
Purpose Undo a commit safely View reference history Rewrite commit history
Rewrites history? ❌ No ❌ No (just logs) ✅ Yes
Safe for shared branches? ✅ Yes ⚠️ Yes (read-only) ❌ No (avoid on shared branches)
Creates new commit? ✅ Yes (undo commit) ❌ No ❌ No (rewrites existing commits)
Used for recovery? ❌ Not mainly ✅ Yes ❌ Not mainly
Used for cleaning history? ❌ No ❌ No ✅ Yes
Works on pushed commits? ✅ Yes ✅ Yes ⚠️ Risky (needs force push)
Typical Use Case Undo a pushed commit Recover lost commit Clean up local commit history
Synonym Undo Safely Time Machine Rewrite History

🔎 When to Use What?

🔵 Use git revert when:

  • You pushed a bad commit
  • You want to safely undo changes
  • You're working in a team
  • You don’t want to rewrite history

Example:

git revert <commit-hash>

🟢 Use git reflog when:

  • You accidentally reset or deleted a branch
  • You lost a commit
  • You need to find old HEAD positions
  • You want to recover something

Example:

git reflog

🟣 Use git rebase when:

  • You want clean commit history
  • You want to squash commits
  • You want linear history
  • You're working locally (before pushing)

Example:

git rebase main

🧠 Pro Tip

Use branches for features and fixes. Keep main clean and production-ready.


Reference

Reference PDF by: supersimple.dev Tutorial Source: https://www.youtube.com/watch?v=hrTQipWp6co

(back to top)