Git is a distributed version control system used to track changes in source code, configuration files, scripts, and other project files.
Developers and DevOps engineers use Git to maintain code history, work on different features, recover older versions, and collaborate safely.
Inshortly
Git tracks every important change made to project files.
It helps developers:
- Save different code versions
- See who changed the code
- Restore older versions
- Work on separate branches
- Merge completed changes
- Collaborate without overwriting work
- Manage application and infrastructure code
Git works mainly on the developer’s local computer and can be used without an internet connection.
How Git Works
Git stores project changes as commits.
A commit represents a snapshot of the project at a specific point in time.
Each commit contains:
- Unique commit ID
- Author name
- Author email
- Date and time
- Commit message
- Project changes
A clear commit history helps teams understand how a project changed and identify when a problem was introduced.
Git Architecture
Git uses four important areas.
1. Working Directory
The working directory is the project folder where you create, modify, or delete files.
2. Staging Area
The staging area contains the changes selected for the next commit.
git add filename
To stage all current changes:
git add .
3. Local Repository
The local repository stores commits, branches, tags, and complete project history.
It is stored inside the hidden .git directory.
git init
4. Remote Repository
A remote repository is a shared copy of the Git project hosted on a platform such as GitHub, GitLab, or Bitbucket.
Git itself manages the versions. The remote platform provides online storage and collaboration.
Basic Git Workflow
Working Directory
↓ git add
Staging Area
↓ git commit
Local Repository
↓ git push
Remote Repository
To receive remote changes:
Remote Repository
↓ git pull
Local Repository and Working Directory
Installing and Configuring Git
Check whether Git is installed:
git --version
Configure your name and email:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Verify the configuration:
git config --list
Git records this identity in each commit.
Creating a Git Repository
Create a project directory:
mkdir my-project
cd my-project
Initialize Git:
git init
The git init command creates a hidden .git directory that stores the repository history and configuration.
Git File States
Git files normally move through four states.
Untracked
A new file that Git is not tracking.
Modified
A tracked file that has been changed.
Staged
A file selected for the next commit.
Committed
A change saved permanently in the local repository history.
Check the current file state:
git status
Essential Git Commands
Initialize a Repository
git init
Clone an Existing Repository
git clone repository-url
Check File Status
git status
Stage One File
git add filename
Stage All Changes
git add .
Create a Commit
git commit -m "Add login feature"
View Commit History
git log
Short history:
git log --oneline
View Commit Details
git show commit-id
Download Remote Changes
git pull origin main
Upload Local Commits
git push origin main
Git Init vs. Git Clone
git init
Use git init when starting a new local project.
git init
git clone
Use git clone when the project already exists in a remote repository.
git clone repository-url
Cloning downloads:
- Project files
- Commit history
- Branch information
- Remote repository configuration
Remember:
New project = git init
Existing project = git clone
What Is a Git Branch?
A branch is an independent line of development.
Branches allow developers to work on new features, bug fixes, or experiments without directly changing the stable main branch.
List branches:
git branch
Create a branch:
git branch feature-login
Create and switch to a branch:
git switch -c feature-login
Switch between branches:
git switch main
A branch is mainly a pointer to a commit. It is not a complete physical copy of the entire repository.
What Is Git Merge?
Merging combines changes from one branch into another.
To merge feature-login into main:
git switch main
git merge feature-login
You should first switch to the branch that will receive the changes.
In this example:
Source branch = feature-login
Target branch = main
What Is a Merge Conflict?
A merge conflict happens when Git cannot automatically combine overlapping changes.
This commonly occurs when two developers modify the same lines differently.
Git may display markers like these:
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-login
To resolve the conflict:
- Open the conflicted file.
- Choose the correct content.
- Remove the conflict markers.
- Save the file.
- Stage the file.
- Commit the resolution.
git add filename
git commit -m "Resolve merge conflict"
What Is .gitignore?
The .gitignore file tells Git which files or directories should not be tracked.
Common examples include:
*.log
*.tmp
.env
build/
node_modules/
Use .gitignore for:
- Log files
- Temporary files
- Build files
- Local configuration
- Environment files
- IDE-generated files
Files already tracked by Git are not automatically ignored after being added to .gitignore.
Undoing Git Changes
Unstage a File
git restore --staged filename
This removes the file from the staging area but keeps its changes in the working directory.
Restore an Uncommitted File
git restore filename
Be careful because this removes uncommitted changes.
Revert a Commit
git revert commit-id
git revert creates a new commit that safely reverses an earlier commit.
It is normally safer for shared branches because it preserves history.
Remove Untracked Files
Preview first:
git clean -n
Delete untracked files:
git clean -f
Always preview before deleting.
Git Reset vs. Git Revert
| Git Reset | Git Revert |
|---|---|
| Moves or changes local history | Creates a new reversing commit |
| Can remove commits from visible history | Preserves the original history |
| Better for local, unpublished work | Safer for shared branches |
| Can be destructive | Usually safer for teams |
For shared repositories, git revert is normally the safer option.
What Are Git Tags?
A tag is a readable label attached to an important commit.
Tags are commonly used for:
- Production releases
- Stable versions
- Application versions
- Important milestones
Create an annotated tag:
git tag -a v1.0 -m "Version 1.0 release"
List tags:
git tag
View tag details:
git show v1.0
Pull Before Push
Before pushing changes, synchronize your branch with the remote repository.
git pull origin main
git push origin main
A practical workflow is:
Pull
↓
Modify files
↓
Add changes
↓
Commit changes
↓
Pull latest changes again
↓
Push commits
Pulling before pushing reduces rejected pushes and unexpected conflicts.
Git in DevOps
Git is a foundational DevOps tool.
DevOps teams use Git to manage:
- Application source code
- Terraform files
- CloudFormation templates
- Ansible playbooks
- Dockerfiles
- Kubernetes manifests
- Helm charts
- Jenkinsfiles
- CI/CD workflow files
- Shell scripts
- Python automation scripts
A typical DevOps workflow looks like this:
Developer changes code
↓
Git tracks the changes
↓
Developer creates a commit
↓
Code is pushed to a remote repository
↓
CI/CD pipeline starts
↓
Application is built, tested, and deployed
Git provides the version history and change control required for automated software delivery.
Git Best Practices
- Create separate branches for features and fixes
- Write clear commit messages
- Make small, focused commits
- Check
git statusbefore committing - Pull the latest changes regularly
- Review changes before staging
- Use
.gitignore - Never commit passwords or private keys
- Avoid rewriting shared branch history
- Create tags for important releases
Common Git Mistakes
Avoid:
- Working directly on the stable branch
- Writing commit messages such as “update”
- Creating very large commits
- Committing passwords or access keys
- Using destructive commands without understanding them
- Ignoring merge conflicts
- Forgetting to pull remote changes
- Staging unnecessary files with
git add . - Deleting the
.gitdirectory accidentally
Important Git Interview Questions
What is Git?
Git is a distributed version control system used to track file changes, maintain project history, and support team collaboration.
Why is Git called distributed?
Git is distributed because each developer can have a complete local copy of the repository and its history.
Explain the Git workflow.
Changes move from the working directory to the staging area using git add, then to the local repository using git commit, and finally to a remote repository using git push.
What is a Git commit?
A commit is a saved snapshot of project changes at a specific point in time.
What is a Git branch?
A branch is an independent line of development used for features, fixes, or experiments.
What is a merge conflict?
A merge conflict happens when Git cannot automatically combine overlapping changes from different branches.
What is the difference between git pull and git push?
git pull downloads and integrates remote changes. git push uploads local commits to a remote repository.
What is the difference between git init and git clone?
git init creates a new local repository. git clone downloads an existing remote repository and its history.
What is .gitignore?
.gitignore contains patterns for files and directories that Git should not track.
What is the difference between git reset and git revert?
git reset changes local history, while git revert creates a new commit that safely reverses an earlier commit.
Beginner Practice Task
Complete this Git practice:
mkdir git-practice
cd git-practice
git init
echo "My Git Project" > README.md
git status
git add README.md
git commit -m "Add project README"
git switch -c feature-update
echo "Learning Git branching" >> README.md
git add README.md
git commit -m "Update README with Git learning"
git switch main
git merge feature-update
git log --oneline
This exercise covers:
- Repository creation
- File tracking
- Staging
- Commits
- Branch creation
- Branch switching
- Merging
- Commit history
Final Summary
Git is a distributed version control system used to track, manage, and recover project changes.
Its most important concepts include:
- Working directory
- Staging area
- Local repository
- Commits
- Branches
- Merging
- Conflict resolution
- Tags
- Remote synchronization
Remember:
Git tracks project changes,
stores version history,
supports branching,
and helps teams manage code safely.