Skip to main content

Command Palette

Search for a command to run...

Git - A Beginner's Guide

Published
3 min read

What is Git?

Git is a Version Control System. It tracks every change we make to our files. If we mess up, we can go back to a previous version instantly. It allows us to work together on the same project without deleting each other's work.

Usages

  • Revert Changes: If we make any mistake or want to go back to a previous stage of our development, we can revert back.

  • Collaboration: We can have 10 people working on the same project at the same time on separate branches.

  • Feature Testing: We can test a new idea in a separate branch without breaking our main working app.

  • Backup: Pushing our code to Global Repository (GitHub/GitLab) saves it in the cloud.

Git Workflow

  1. The Working Directory

    • Represents the current state of the files on our local filesystem.

    • This is where we actively write code, refactor functions, and modify assets.

    • Changes here are either Modified (changed but known to Git) or Untracked (new files Git has never seen).

  1. The Staging Area (The Index)

    • The Staging Area acts as a buffer between our working files and the permanent repository.

    • It allows us to draft our next commit. We might have modified ten files, but we only want to include three of them in a specific update.

    • When we run git add, we are updating the Index.

  1. The Local Repository (HEAD)

    • This is the hidden .git directory inside our project root which serves as our personal database of the project's history.

    • When we run git commit, we record a permanent snapshot of the Staging Area into this database.

    • The latest commit in our current branch is referred to as HEAD.

  1. The Remote Repository (Upstream)

    • This is the copy of the repository hosted on a remote server (e.g., GitHub, GitLab).

    • We synchronize our Local Repository with the Remote Repository using push (upload) and pull (download) commands.

    • It provides redundancy (backup) and facilitates collaboration.

    • Our local changes remain private until we explicitly push them to the remote, allowing us to work offline or draft feature branches without affecting the team's production code.

Git Commands - Cheat Sheet

1. Setup

2. Starting a Project

  • git init — We turn our current folder into a Git repository.

  • git clone <url> — We download an existing project from the internet.

3. Save & Sync

  • git status — Shows us which files are changed or staged.

  • git add . — We move all changed files to the Staging Area.

  • git add <file> — We move just one specific file to the Staging Area.

  • git commit -m "message" — We save the staged files permanently.

  • git push — We upload our commits to the cloud (GitHub/GitLab).

  • git pull — We download new changes from the cloud to our computer.

4. Branching

  • git branch — Lists all our branches.

  • git branch <name> — We create a new branch.

  • git checkout <name> — We switch to a branch.

  • git checkout -b <name> — We create and switch to a new branch.

  • git merge <name> — We combine the specified branch into our current one.

5. Auditing

  • git log — Shows a list of all our past commits (history).

  • git reset HEAD~1 — We undo the last commit (but keep our changes).

  • git stash — We temporarily hide our changes to work on something else.

  • git stash pop — We bring back the hidden changes.