Skip to main content

Git Basics

September 30, 2025About 3 min

Git Basics

Setting Up Your Repository

The first steps involve telling Git that you want to start tracking a project. A repository (often shortened to "repo") is the .git folder that holds all your project's history and metadata.

CommandActionExplanation
git initInitializeRuns once inside your project folder to create the hidden .git subdirectory. This transforms the folder into a local Git repository, ready to be tracked.
git statusCheck StateA command you'll use constantly. It shows the current state of your Working Directory and Staging Area, listing files that are untracked, modified, or staged for the next commit.
git configConfigureSets your user identity for the commits you make. This is required before your first commit.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

The Core Workflow: Track and Commit

Git's local version control loop involves three steps: Modify, Stage, and Commit. This process creates the historical snapshots that form the backbone of your project's timeline.

Step 1: Modifying Files (Working Directory)

You simply edit, create, or delete files in your project folder. git status will show these files as modified or untracked.

Step 2: Staging Changes (Staging Area / Index)

The Staging Area is a crucial intermediate step. It allows you to precisely curate which changes will be included in the very next snapshot.

CommandActionExplanation
git add <file>Stage Specific FileAdds the changes from a specific file into the Staging Area.
git add .Stage All ChangesA common shortcut that stages all modified and untracked files in the current directory and its subdirectories.

Step 3: Committing (Git Directory)

A commit is the actual act of taking the snapshot. Everything currently in the Staging Area is bundled together, saved to the .git directory, and given a unique ID (a SHA-1 hash).

CommandActionExplanation
git commit -m "message"SnapshotPermanently records the staged changes. The -m flag is essential for providing a descriptive message that explains why the changes were made, helping you and others understand the history later.
git commit -am "message"Quick CommitA shortcut that combines staging and committing, but only for files that Git is already tracking. It bypasses the explicit staging of modified files.

Connecting to the Remote World (GitHub/GitLab/BitBucket/...)

To back up your work and collaborate, you need to connect your local repository to a remote repository—typically hosted on a service like GitHub or GitLab.

CommandActionExplanation
git remote add origin <URL>Link RemoteTells your local repo the URL of the external (remote) server where the code will be hosted. origin is the standard nickname for this primary remote.
git pushSend ChangesTransfers your committed changes from your local repository to the remote repository. This command is how you back up your work and make it available to collaborators.
git push -u origin mainSend changesFirst Push 🚀. The first time you push, you use the -u (or --set-upstream) flag to tell Git to permanently link your local main branch to the remote origin/main branch. All subsequent pushes can just be git push.
git pullGet ChangesDownloads and integrates changes from the remote repository into your local repository. This is how you update your local copy with work done by others (or by yourself on another machine).

Reviewing and Undoing

Being a time machine, Git gives you tools to inspect your history and easily undo mistakes.

CommandActionExplanation
git logView HistoryShows a chronological list of all commits, including the author, date, and commit message. This lets you see the project timeline.
git show <commit-id>Inspect SnapshotDisplays the full details of a specific commit, including exactly what was added and removed in that snapshot. You can use the first few characters of the commit's SHA ID.
git diffCompare ChangesShows differences between the Working Directory and the Staging Area (what you've changed but haven't staged).
git diff --stagedCompare ChangesShows differences between the Staging Area and the last commit (what you are about to commit).
git restore --staged <file>UnstageMoves a file out of the Staging Area back to the Working Directory. Useful if you staged a file by mistake.
git checkout -- <file>Discard ChangesDANGEROUS ⚠️. Discards all local, unstaged changes to a file, reverting it to the state it was in at the last commit. This effectively deletes your work since the last commit.