Git Basics from A to Z
This article explains Git in simple, practical language. It covers the core ideas and the commands most developers use every day.
What Git is
Git is a version control system. It tracks changes in files and lets developers move through different versions of a project.
Think of Git as a time machine for code. It helps with:
- checking what changed
- recovering older versions
- working on multiple features at once
- collaborating with other developers
Why teams use Git
In production work, Git helps teams keep a clear record of changes. It also makes code reviews and rollback easier.
A typical service team may have:
- main for stable code
- develop for integration work
- feature branches for individual tasks
This structure keeps changes organized and reduces conflict.
Install Git
On Windows, use Git for Windows from the official Git website.
Check the installation:
git --version
Example output:
git version 2.45.0.windows.1
Initialize a repository
Create a folder and start a Git repo:
mkdir my-project
cd my-project
git init
This creates a hidden .git folder that stores repository metadata.
Check repository status
git status
This shows:
- modified files
- untracked files
- changes staged for commit
- branch information
Basic workflow
git add file.txt
git commit -m "Add initial file"
A commit is a saved snapshot of the project.
Example
echo "Hello from Git" > hello.txt
git add hello.txt
git commit -m "Add hello file"
The commit message should describe the change clearly.
Add and commit
The typical flow is:
git add .
git commit -m "Implement login validation"
Important rule:
- git add selects changes to include
- git commit records them
The .gitignore file
A .gitignore file tells Git which files should not be tracked.
Example:
node_modules/
.env
bin/
*.log
This is useful for:
- local secrets
- dependency folders
- temporary files
- build outputs
View history
git log
This shows commit history with messages and hashes.
You can make it easier to read:
git log --oneline --decorate --graph --all
See changes between versions
git diff
This shows file changes not yet committed.
To compare a committed version:
git diff HEAD~1 HEAD
The meaning of git add, git commit, git push
A beginner-friendly way to remember it:
- git add = stage the work
- git commit = save the staged work locally
- git push = upload the saved work to a remote repository
Connect to a remote repository
If the repository is on GitHub, GitLab, or Azure Repos:
git remote add origin https://github.com/your-user/my-project.git
git push -u origin main
The -u flag sets the branch tracking relationship.
Cloning a repository
git clone https://github.com/your-user/my-project.git
This downloads the repository and creates a working copy on the local machine.
Production example
A team may use Git to track app code, infrastructure scripts, and deployment manifests together. Each feature gets its own branch and each change is reviewed before merging.
Common pitfalls
- committing generated files that should be ignored
- writing vague commit messages like "update"
- pushing without checking branch state
- forgetting to pull before starting work on a shared branch
Key takeaways
- Git tracks file history and collaboration.
- git add and git commit are the basics of saving work.
- .gitignore prevents accidental tracking of noise.
- git log and git diff help understand history and changes.