GIT - Global Information Tracker

1. What is Git?

Git is a distributed version control system (DVCS) that helps track changes in code and coordinate work between multiple people.
It works locally on your machine and allows you to:

  • Save snapshots of your code (commits).
  • View and revert changes.
  • Work in multiple branches for different features.
  • Merge changes from different sources.

Key points about Git:

  • Distributed → Every developer has the full history of the project.
  • Snapshots → Git stores snapshots, not just file differences.
  • Branching → Create and switch between versions of code easily.
  • Merging & Rebasing → Combine work from different branches.

2. Git Core Concepts

Repository (Repo)

A directory containing:

  • Your code.
  • A hidden .git folder (stores Git’s history and metadata).

Commit

A saved version of your project at a specific time.

Branch

A parallel line of development in Git.
Example: main, dev, feature-xyz.

Remote

A version of your repository hosted elsewhere (like on GitHub).

A pointer to your current commit/branch.


3. What is GitHub?

GitHub is a cloud-based hosting service for Git repositories.
It adds features on top of Git:

  • Remote repository hosting.
  • Collaboration tools (Pull Requests, Issues, Discussions).
  • Access control and team management.
  • CI/CD integrations.

Key difference:

  • Git → The tool for version control.
  • GitHub → A platform to store, share, and collaborate using Git.

4. Git Workflow Overview

  1. Local Work (in your machine):

    • Edit files.
    • Stage changes → git add (to stage all deletions in one go use git add -u)
    • Commit changes → git commit
  2. Sync with Remote:

    • Pull latest changes → git pull
    • Push your changes → git push
  3. Collaboration:

    • Create branches for features or fixes.
    • Merge changes via Pull Requests (PRs) or locally.
    • Resolve conflicts if multiple people edit the same lines.

🧠 Git Command Cheat Sheet

🔧 SETUP & CONFIGURATION

CommandDescription
git --versionCheck Git version
git config --global user.name "Your Name"Set Git username
git config --global user.email "you@example.com"Set Git email
git config --global core.editor codeSet default editor (like VS Code)
git config --listSee current Git settings
git config --global push.autoSetupRemote trueto always set upstream automatically on first push.
Then git push on a new branch will automatically do the linking.

📁 REPOSITORY MANAGEMENT

CommandDescription
git initCreate a new local Git repo
git clone <url>Clone remote repo to local
cd folder_name
git clone <url> .
then the folder becomes the repo root.
git remote add origin <url>Add remote repo (Now your local repo knows that origin = that GitHub repo,
it's just the nickname for the url (repo), could be anything prod,
upstream, github,etc.)
git remote set-url origin <url>To change the origin url
git remote rename origin githubchanging name of remote from origin to github
git remote remove originTo delete the link to remote repo
git remote -vShow remote URLs

📊 STATUS & INFO

CommandDescription
git statusShow current status (changes, staged files, etc.)
git logShow commit history
git log --onelineCompact log
git diffShow unstaged changes
git diff --stagedShow staged changes

📥 STAGING & COMMITTING

CommandDescription
git add <file>Stage a specific file
git add .Stage all changes
git commit -m "message"Commit staged changes
git commit -am "message"Stage and commit tracked files only
git restore <file>Undo changes in working directory
git reset <file>Unstage a file
git reset --hardReset working and staging to last commit, means you told git
to thow away all uncommitted changes in both working and staging area
If you're file has uncommitted changes then it's gone.

🔁 BRANCHING & MERGING

CommandDescription
git branchList branches
git branch <name>Create a branch
git checkout <name>Switch to branch
git checkout -b <name>Create + switch
git merge <branch>Merge into current branch
git rebase <branch>Reapply commits onto another branch
git branch -d <name>Delete branch

NOTE: Consider two branches main and testing, when you're running rebase, i.e., git rebase testing (inside main branch), then what it does is base as testing, main’s commits are temporarily detached from the branch and being applied onto testing. if we consider two branches as main and feature, then rebasing on main is blowing away the feature branch commits, and dupicating them as new commits on the top of the main branch.

AFTER REBASING: When you try to push, Git says:

“Your branch history is different from the remote — I won’t overwrite without your permission.” two options two solve this:

  • Force push (safe only if you’re the only one working on this branch)
    git push -f or git push --force-with-lease
  • Don’t rewrite history (merge instead)
    git pull --rebase=false
    then, git push

📤 PUSHING & PULLING

CommandDescription
git pushPush changes to remote
git push origin <branch>Push branch to remote
git pullFetch + merge changes from remote
git fetchFetch changes without merging
git push -u origin <branch-name>If you only want to set upstream for a specific branch, -u (or --set-upstream)

NOTE: - When you clone a repo, your default branch (often main or master) already has an upstream — it’s linked to the remote’s same branch.

  • But when you make a new local branch: it’s not automatically connected to a remote branch. (last bullet).
  • Note for each new repo, you have to add upstream or remote.
  • Note if you're using ssh key for your system, then don't just clone using search bar, clone usign ssh url only.

🚑 UNDO & CLEANUP

CommandDescription
git reset --hard HEADUndo all local changes
git revert <commit>Undo a specific commit (safely)
git stashSave changes temporarily
git stash popReapply stashed changes
git clean -fdRemove untracked files/directories

🧪 TAGGING & RELEASES

CommandDescription
git tagList tags
git tag <name>Create tag
git tag -a <name> -m "message"Annotated tag
git push origin <tagname>Push tag to remote

🧠 INSPECTION & DEBUGGING

CommandDescription
git show <commit>Show details of a commit
git reflogShow history of HEAD movements
git blame <file>Show who edited each line
git bisectFind commit that introduced a bug

🔧 ADVANCED (OPTIONAL)

CommandDescription
git cherry-pick <commit>Apply a specific commit
git archiveExport repo to zip/tar
git submoduleManage nested repos
git worktreeManage multiple working directories

NOTE: git cherry-pick lets you take a specific commit from any branch and apply it onto your current branch — without merging the entire branch.
Think of it like:
I don’t want the whole cake, just that one slice.