Git for Beginners: Basics and Essential Commands

Basic Concept
Repository (Repo): The project folder that Git tracks, containing all project files and the hidden
.gitfolder with all the version history and metadata.Working Directory: The actual files you are currently editing on your local machine.
Staging Area (Index): An area where you prepare changes to be committed. You explicitly add files here before recording them in the repository.
Commit: A "save point" or snapshot of your staged changes at a specific time, complete with a unique ID, author information, date, and a descriptive message.
Branch: A parallel line of development that lets you work on new features in isolation from the main project code (often the
mainormasterbranch).Remote Repository: A version of your project hosted on an internet server (e.g., GitHub, GitLab), used for collaboration and backup.
Essential Git Commands
Here are the essential commands for a beginner's workflow:
| Command | Description | Example |
git config | Sets your username and email for commit identification. | git config --global user.name "Your Name" |
git init | Initializes a new local Git repository in the current directory. | git init |
git clone | Creates a local working copy of an existing remote repository. | git clone [url] |
git status | Shows the current state of your repository, including modified, staged, and untracked files. | git status |
git add | Adds file changes to the staging area, preparing them for the next commit. | git add [filename] or git add . (for all files) |
git commit | Records the staged changes as a new commit in the local repository's history. | git commit -m "Descriptive message" |
git push | Uploads your local commits to the remote repository. | git push origin [branch-name] |
git pull | Fetches changes from the remote repository and merges them into your current local branch. | git pull origin [branch-name] |
git branch | Lists, creates, or deletes branches. | git branch [new-branch-name] |
git checkout or git switch | Switches between different branches or commits. | git switch [branch-name] |
git merge | Combines changes from a specified branch into your current active branch. | git merge [branch-name] |
git log | Displays the commit history of the repository. | git log |
What is Git
Git is a version control system - software that tracks changes to files over time. It's one of the most widely used tools in software development.
What Git does
Git helps you keep a complete history of your code and files. Every time you save a snapshot (called a "commit"), Git records what changed, who changed it, and when. This means you can:
Go back to any previous version of your files
See exactly what changed between versions
Work on multiple features simultaneously using "branches"
Collaborate with others without overwriting each other's work
Merge different people's changes together
Local repository structure overview
A local Git repository structure consists of three main trees: the Working Directory (actual files), the Staging Index (prepared changes), and the Git Repository/HEAD (committed history, located in the .git folder). It allows individual developers to commit, branch, and manage history locally before pushing to remote.
Commit history flow
Git commit history represents a chronological, linked-list snapshot of project changes, typically managed through branching and merging to track development flow. It flows from working directory changes, to staging (git add), and finally to a permanent local repository commit (git commit), forming a DAG (Directed Acyclic Graph).
Key Aspects of Commit History Flow:
Linear History: A straightforward, sequential list of commits, often seen in simple branches or with rebasing.
Branched/Merged History: A more complex flow where features are developed in isolated branches and merged back into main lines (like
mainordevelop), often visualized with--graph.Git Flow Model: A popular workflow using strict branching for production (main) and development (
develop), feature, release, and hotfix branches to manage history.
