Version control is the backbone of modern development, and Git stands out as the industry favorite! Master Git to track changes, collaborate efficiently, and streamline your DevOps workflows.
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP
Learning points:
- What is Version Control? β Why Git is the most popular VCS.
- Git vs. Other VCS β Differences from SVN, Mercurial.
- Repositories, Commits, and Branches β How Git tracks changes.
- Git States β Working directory, staging area, and commit history.
- Basic Git Commands β init, add, commit, status, log, diff.
- Branching & Merging β Creating branches and merging changes.
- Pull Requests (PRs) β Understanding GitHub workflows.
- Remote Repositories β Connecting local Git with GitHub (git remote add origin).
- Collaboration β Cloning, fetching, pulling, and pushing changes.
- Resolving Merge Conflicts β How to handle and fix conflicts.
π§ Learn here:
Git Full Course Part 1 - Covers Installation, Key Concepts, Demo with GitHub
What is Version Control?
Version Control System (VCS) is a tool that helps track changes in code over time, collaborate with team members, and manage code history.
- Enables rollbacks to previous versions.
- Tracks who made what changes and when.
- Facilitates parallel development via branching.
Why Git is the Most Popular VCS
- Distributed System β Every developer has a full copy of the repo.
- Lightning Fast β Operations like commit, diff, and branch are local.
- Flexible Branching β Create, merge, or delete branches easily.
- Open-source and supported by platforms like GitHub, GitLab, Bitbucket.
Git vs. Other VCS
| Feature | Git | SVN | Mercurial |
|---|---|---|---|
| Model | Distributed | Centralized | Distributed |
| Branching | Lightweight & fast | Heavyweight | Lightweight |
| Speed | Very fast | Slower (network dependent) | Fast |
| Popularity | πππππ | ππ | ππ |
| Usage in DevOps/CICD | Industry standard | Legacy support | Rare |
π Git Concepts: Repositories, Commits, Branches
- Repository (Repo) β A project tracked by Git.
- Commit β A snapshot of code changes.
- Branch β A parallel line of development.
# Create a Git repo
git init
# Check repo status
git status
π Git States: How Git Tracks Code
- Working Directory β Your local files.
- Staging Area β Changes marked for commit.
- Commit History β Confirmed snapshots.
# Stage changes
git add .
# Commit staged changes
git commit -m "Initial commit"
Basic Git Commands
git init # Initialize a repo
git add <file> # Stage files
git commit -m "msg" # Commit staged files
git status # Show current state
git log # View commit history
git diff # Show unstaged changes
Branching & Merging
git branch <branch> # Create a new branch
git checkout <branch> # Switch to the branch
git merge <branch> # Merge branch into current branch
Branches are isolated workspacesβideal for features or fixes. Merging integrates changes back.
Pull Requests (PRs) & GitHub Workflows
A Pull Request (PR) is a request to merge one branch into anotherβtypically reviewed by teammates.
GitHub workflow:
- Fork repo (if needed)
- Create a branch
- Push code
- Create PR
- Review β Merge
Remote Repositories
Connect your local repo to GitHub:
git remote add origin https://github.com/user/repo.git
git push -u origin main
Git Collaboration Basics
git clone <repo_url> # Clone remote repo
git fetch # Fetch remote changes
git pull # Fetch + merge remote changes
git push # Push local commits
Resolving Merge Conflicts
When two branches edit the same lines:
- Git shows conflict markers (
<<<<<<<,=======,>>>>>>>). - Manually edit the file.
- Mark it resolved:
git add conflicted_file
git commit
Tip: Use git mergetool for visual help.
ποΈ Resources:
Initial Tasks:
β Install Git on your system: Git Installation
β
Configure your Git username and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
β
Create a new folder and initialize a Git repository:
mkdir git-project && cd git-project
git inittouch index.html style.css
git add .
git commit -m "Initial commit"
β
Modify index.html, check changes, and make another commit:
git status
git diff
git add index.html
git commit -m "Updated index.html"
β
Check commit history using:
git log --oneline
β
Create a GitHub Repository, add a remote, and push:
git remote add origin https://github.com/yourusername/git-project.git
git branch -M main
git push -u origin mainπ₯ Challenges
πΉ Challenge 1: Fork and clone an open-source project (Sample Project Link)
πΉ Challenge 2: Create a new branch (feature-branch), switch, and commit changes.
πΉ Challenge 3: Merge feature-branch into main and push the changes.
πΉ Challenge 4: Use git reset or git revert to undo a commit.
πΉ Challenge 5: Rebase feature-branch onto main and understand the difference.
πΉ Challenge 6: Create a PR on GitHub from a feature branch.
πΉ Challenge 7: Create conflicting changes in different branches and resolve them.
πΉ Challenge 8: git stash to save and restore uncommitted changes.
πΉ Challenge 9: Add version tags to commits and push them.
πΉ Challenge 10: Use git commit --amend and git rebase -i to edit past commits.
Solutions:
β
Challenge 1: Fork and clone an open-source project (Sample Project Link)
Answer:
Step 1: Fork the Repository
- Go to the repository link:
π EKS Auto Mode Workshop - Click the "Fork" button (top right) to create a copy under your GitHub account
Step 2: Clone the Forked Repository
Once you've forked the repository, run:
(Replace YOUR_GITHUB_USERNAME with your actual GitHub username.)
git clone https://github.com/YOUR_GITHUB_USERNAME/eks-auto-mode-workshop.git
#e.g. git clone https://github.com/sd031/eks-auto-mode-workshop.gitBonus π‘ : If you want to sync future updates from the original repository, navigate into the Project Directory & Set the Upstream Repository, and run:
cd eks-auto-mode-workshop
git remote add upstream https://github.com/sd031/eks-auto-mode-workshop.gitVerify via running:
π Check Git Remotes (git remote -v)
git remote -v
Example Output:
origin https://github.com/YOUR_GITHUB_USERNAME/eks-auto-mode-workshop.git (fetch)
origin https://github.com/YOUR_GITHUB_USERNAME/eks-auto-mode-workshop.git (push)
upstream https://github.com/sd031/eks-auto-mode-workshop.git (fetch)
upstream https://github.com/sd031/eks-auto-mode-workshop.git (push)
To fetch and merge updates from the original repo:
git fetch upstream
git merge upstream/main
β
Challenge 2: Create a new branch (feature-branch), switch, and commit changes.
Answer:
Step 1: Create a New Branch and Switch to It:
checkout -b β Creates and switches to feature-branch.
git checkout -b feature-branchStep 2: Verify the Active Branch
(The * indicates the current branch.)
git branch
# output (the * marks the current branch):
# main
# * feature-branchStep 3: Make Some Changes
Edit a file or create a new one:
echo "New feature added!" > feature.txtStep 4: Stage the Changes
git add feature.txtStep 5: Commit the Changes
git commit -m "Added feature.txt with new feature"Step 6: Push the New Branch to GitHub
git push origin feature-branchβ Challenge 3: Merge feature-branch into main and push the changes.
Step 1: Switch to the main Branch
git checkout mainStep 2: Ensure Your main Branch is Up-to-Date
git pull origin mainThis fetches and merges the latest changes from the remote repository.
Step 3: Merge feature-branch into main
git merge feature-branchIf there are no conflicts, youβll see:
Updating 1a2b3c4..4d5e6f7
Fast-forward
feature.txt | 1 +
1 file changed, 1 insertion(+)If there are conflicts, Git will notify you, and youβll need to manually resolve them.
Step 4: Push the Merged Changes to GitHub
git push origin mainStep 5: Delete feature-branch
After merging, you can delete the feature branch:
ποΈ Delete a Git Branch Locally and Remotely
# Delete a local branch
git branch -d feature-branch
# Delete a remote branch
git push origin --delete feature-branch
Example Output:
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Updating 1a2b3c4..4d5e6f7
Fast-forward
feature.txt | 1 +
1 file changed, 1 insertion(+)
Deleted branch feature-branch (was 4d5e6f7).β Challenge 4: Use git reset or git revert to undo a commit.
Answer :
Using git reset:
Completely Remove the Last Commit
git reset --hard HEAD~1Explanation:
--hard β Removes the commit and discards changes.
If you want to undo the commit but keep the changes:
git reset --soft HEAD~1Using git revert to Undo a Specific Commit Without Changing History
git revert <commit-hash>Example:
git log --oneline to find the commit hash, then revert that commit (Git adds a new commit that undoes it):
π View Commit History & Revert a Commit
# Show concise commit history
git log --oneline
Example Output:
d3f3c12 Added a new feature
b2a1f98 Fixed a bug
# Revert a specific commit by hash
git revert d3f3c12
Bonus π‘ If you've already pushed the commit to GitHub and need to force-push:
git push --force-with-lease origin main # rewrites the remote branch: use with cautionβ Challenge 5: Rebase feature-branch onto main and understand the difference.
Answer:
First what is exactly a Git Rebase:
git rebase is a command that moves or combines commits from one branch onto another, rewriting commit history in a linear way.
Example:
π Git Rebase Visualization (Before & After)
π Before Rebase (git log --oneline --graph)
* commit C (feature-branch)
* commit B (feature-branch)
| * commit M2 (main)
| * commit M1 (main)
|/
* commit A (common ancestor)
β
After Rebase (git log --oneline --graph)
* commit C' (feature-branch, rebased)
* commit B' (feature-branch, rebased)
* commit M2 (main)
* commit M1 (main)
* commit A (common ancestor)
π§commit Bandcommit Care now reapplied on top ofmain, creating new commit hashesB'andC'.
Now, to rebase feature-branch onto main using the following steps:
Step 1: Switch to feature-branch
git checkout feature-branchStep 2: Rebase feature-branch onto main
git rebase mainExplanation:
- This moves feature-branch to start from the latest main commit.
- It applies feature-branch commits on top of main.
Step 3: Resolve Any Merge Conflicts
If conflicts occur:
- Git will pause and show which files have conflicts.
- Manually edit the conflicting files to resolve them.
- Mark conflicts as resolved:
git add <resolved-file> - Continue the rebase:
git rebase --continue - If you want to abort the rebase:
git rebase --abort
π Difference: Rebase vs Merge
| Feature | Rebase (git rebase main) |
Merge (git merge main) |
|---|---|---|
| History | Creates a clean linear history | Keeps a branching history |
| Commits | Moves commits on top of the latest main |
Merges main changes with a merge commit |
| Conflicts | Can require resolving multiple conflicts | Resolves conflicts once at merge |
| Recommended When | You want a clean history (before merging) | You need a clear branch history |
β Challenge 6: Create a PR on GitHub from a feature branch.
Answer:
We can create a Pull Request (PR) on GitHub from a feature-branch using the following steps:
Step 1: Push the Feature Branch to GitHub
If your feature branch isn't pushed yet, run:
git push origin feature-branchStep 2: Open GitHub and Navigate to the Repository
- Go to your GitHub repository.
- Click on the "Compare & pull request" button (it appears after pushing the branch).
Step 3: Fill in PR Details
- Ensure the correct base and compare branches:
- Base branch:
main - Compare branch:
feature-branch
- Base branch:
- Add a Title & Description (e.g., "Added new feature for user authentication").
- Optional: Assign reviewers, add labels, or link issues.
Step 4: Create the PR
- Click "Create Pull Request".
- Your PR is now open for review.
Step 5: Merge the PR (After Approval)
Once approved, merge the PR:
- Click "Merge pull request".
- Select "Squash and merge" (optional for a clean history).
- Delete the feature branch if no longer needed.
Bonus π‘ : Create a PR Using GitHub CLI (Optional)
gh pr create --base main --head feature-branch --title "New Feature" --body "Description of the feature"(Requires GitHub CLI: gh auth login before using this command)
β Challenge 7: Create conflicting changes in different branches and resolve them.
Step 1: Create a Repository (or Use an Existing One)
If you donβt have a repository, create one:
π Initialize a Git Repository and Make First Commit
# Create and enter a new project directory
git init -b main conflict-demo
cd conflict-demo
# Create a file and add content
echo "Hello, DevOps!" > file.txt
# Stage the file
git add file.txt
# Commit the file
git commit -m "Initial commit"
Step 2: Create and Modify feature-branch
# Create and switch to a new branch
git checkout -b feature-branch
# Modify the file
echo "Feature branch change" > file.txt
# Stage the changes
git add file.txt
# Commit the changes
git commit -m "Modified file.txt in feature-branch"
Step 3: Switch to main and Make a Conflicting Change
π Update main Branch and Commit Changes
# Switch to the main branch
git checkout main
# Modify the file
echo "Main branch change" > file.txt
# Stage the change
git add file.txt
# Commit the change
git commit -m "Modified file.txt in main"
Now, file.txt has different content in main and feature-branch, causing a merge conflict.
Step 4: Merge feature-branch into main
git merge feature-branchYouβll see a conflicting message like:
β οΈ Merge Conflict Example
After attempting to merge:
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
Step 5: Resolve the Conflict
Open file.txt, which will look like this:
<<<<<<< HEAD
Main branch change
=======
Feature branch change
>>>>>>> feature-branch
Manually edit the file to keep the correct content.
Mark the conflict as resolved:
git add file.txt
git commit -m "Resolved merge conflict in file.txt"Step 6: Push the Merged Changes
git push origin mainβ Challenge 8: git stash to save and restore uncommitted changes.
Answer:
First, what is git stash ?
git stash to temporarily save and restore uncommitted changes
Now, to answer the usage here is with an example:
Step 1: Make Some Changes to a File
echo "Uncommitted changes" >> file.txtCheck the status using git status command:
git status
# Changes not staged for commit:
# modified: file.txtStep 2: Stash Uncommitted Changes
git stashThis saves the changes and reverts your working directory to the last committed state.
Check the status again using git status the command:
git statusNothing to commit, working tree clean.
Step 3: Restore the Stashed Changes
To restore the last stashed changes:
git stash popOR if you want to apply the changes but keep them in stash:
git stash applyStep 4: List All Stashes
If you have multiple stashes:
π¦ View Git Stashes (git stash list)
git stash list
Example Output:
stash@{0}: WIP on feature-branch: 123abc Modified file.txt
stash@{1}: WIP on main: 456def Added a new feature
π‘ Usegit stash apply stash@{0}orgit stash pop stash@{0}to reapply or remove and apply a stash.
Step 5: Restore a Specific Stash
If you want to apply a specific stash:
git stash apply stash@{1}Step 6: Delete a Stash
After restoring, if you no longer need the stash:
git stash drop stash@{0}To clear all stashes:
git stash clearβ Challenge 9: Add version tags to commits and push them.
Answer:
Letβs first understand what is Git Version Tags :
Git version tags are pointers to specific commits, commonly used for marking release versions (e.g., v1.0.0). Tags help in tracking stable versions of the codebase and are immutable references. They come in two types: lightweight (a simple reference) and annotated (with metadata like message, author, and timestamp).
hereβs how you work with it:
Step 1: Check the Latest Commit
To verify which commit youβre tagging:
π§Ύ Visualize Git History with Graph and Decorations
git log --oneline --graph --decorate -n 5
Example Output:
* a1b2c3d (HEAD -> main) Added feature X
* e4f5a6b Fixed bug Y
* c7d8e9f Initial commit
π§--graphshows the branch structure
π·οΈ--decorateshows branch pointers likeHEADormain
π’-n 5limits the output to the latest 5 commits
Step 2: Create a Tag
- Lightweight Tag (Basic, No Message)git tag v1.0.0
- Annotated Tag (With Message)
git tag -a v1.0.0 -m "Release version 1.0.0"
Step 3: List Tags
git tag
# v1.0.0
# details of an annotated tag:
git show v1.0.0Step 4: Push the Tag to GitHub
git push origin v1.0.0To push all tags:
git push --tagsStep 5: Delete a Tag (If Needed)
- Delete a Local Tag
git tag -d v1.0.0 - Delete a Remote Tag
git push origin --delete v1.0.0
Bonus π‘ : Checkout a Specific Tag
To create a branch from a tag:
git checkout -b release-1.0 v1.0.0β
Challenge 10: Use git commit --amend and git rebase -i to edit past commits.
Answer:
Step 1: Modify a Tracked File
echo "New change" >> file.txt
git add file.txtStep 2: Amend the Last Commit
git commit --amend -m "Updated commit with new changes"β This replaces the last commit with an amended one (it gets a new hash) instead of adding another commit.
Step 3: Push the Amended Commit (If Already Pushed)
If you already pushed the commit, force push is required:
git push --force-with-lease origin mainπ¨ Use with caution, as it rewrites history!
Option 2: Edit Older Commits (git rebase -i)
If you need to modify a commit that is not the last one, use interactive rebase.
Step 1: List Recent Commits
git log --oneline -n 5Example output:
πΏ Git Commit Log with Feature Branch
git log --oneline --graph --decorate -n 5
Example Output:
* a1b2c3d (HEAD -> feature-branch) Added new feature
* e4f5a6b Fixed API issue
* c7d8e9f Updated documentation
* 0a1b2c3 Initial commit
π§HEAD -> feature-branchshows the current branch pointer
π‘ Useful for reviewing recent history and branch context
Step 2: Start an Interactive Rebase
To modify the last 3 commits:
git rebase -i HEAD~3Step 3: Modify Commits:
The editor will show
βοΈ Git Interactive Rebase Example
# oldest commit first (the reverse of git log)
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue
pick a1b2c3d Added new featureYou can change pick to:
rewordβ change the commit messageeditβ amend the commitsquashβ combine commitsdropβ remove a commit
π After editing, save and close the file to continue rebase.
Change pick to:
π οΈ Git Interactive Rebase with reword and edit
pick c7d8e9f Updated documentation
edit e4f5a6b Fixed API issue
reword a1b2c3d Added new featureWhat it does:
reword: lets you change the commit message fora1b2c3dedit: pauses the rebase aftere4f5a6bso you can change the content or amend the commitpick: keeps the commitc7d8e9fas-is
β After editing:
# For reword: Git will prompt for a new message
# For edit:
# Make your changes, then run:
git add .
git commit --amend
git rebase --continue
Save and close the editor.
Step 4: Edit the Selected Commits
For reword, Git will prompt you to edit the commit message.
For edit, Git will pause the rebase:
- Modify the commit (if needed) :
echo "Extra fix" >> file.txt
git add file.txt
git commit --amend - Continue the rebase:
git rebase --continue
Step 5: Push the Edited Commits
If you already pushed the commits, force push is required:
git push --force-with-lease origin feature-branchBonus Read π‘ : Whatβs inside the .git folder?
π .git/
βββ π branches/ # Legacy, usually empty in modern Git
βββ π hooks/ # Client-side hooks for automating tasks (pre-commit, post-merge etc.)
βββ π info/ # Contains exclude file for local ignores
β βββ π exclude # Like .gitignore but not committed
βββ π logs/ # Records history of ref updates
β βββ π refs/
β β βββ π heads/
β β β βββ π main # Log of changes to main branch
β β βββ π remotes/
β β βββ π origin/
β β βββ π main # Log of changes to remote tracking branch
β βββ π HEAD # Log of changes to HEAD
βββ π objects/ # Stores all Git objects (commits, trees, blobs)
β βββ π info/
β βββ π pack/ # Stores packed objects for efficiency
βββ π refs/ # Pointers to commit objects
β βββ π heads/
β β βββ π main # Pointer to latest commit on main
β βββ π remotes/
β β βββ π origin/
β β βββ π main # Tracks remote origin/main
β βββ π tags/ # Tag references
βββ π config # Project-specific Git configuration
βββ π description # Description of the repo (used by GitWeb)
βββ π HEAD # Points to the current branch ref
βββ π index # Staging area (cached file snapshot)
βββ π COMMIT_EDITMSG # Stores commit message during commit
βββ π FETCH_HEAD # Records the branch fetched from a remote
βββ π ORIG_HEAD # Points to previous HEAD (useful after rebase, merge, etc.)
βββ π MERGE_HEAD # Present during a merge; points to the commit being mergedThis .git folder is what makes a directory a Git repository. If deleted, you lose version control. π
Explanation of Each File and Folder:
- branches/ (π)
- This was historically used to store branch references but is now obsolete.
- hooks/ (π)
- Contains shell scripts that automate actions (e.g., pre-commit, post-commit, pre-push).
- Default ones are .sample files to guide users.
- info/ (π)
- Contains an exclude file for repository-specific ignored files (like .gitignore but local).
- logs/ (π)
- Tracks movements of HEAD and branches.
- Helps in recovering lost commits (git reflog).
- objects/ (π)
- Stores actual data as blobs (file contents), trees (directories), commits, and tags.
- Uses SHA-1 hashed directories for efficient storage.
- pack/ stores compressed data to optimize space.
- refs/ (π)
- Stores branch (heads/), remote (remotes/), and tag (tags/) references.
- These files contain commit hashes pointing to the latest commit.
- COMMIT_EDITMSG (π)
- Stores the last commit message (useful when reusing the commit message).
- config (π)
- Stores repository-level configurations like remotes, user info, etc.
- description (π)
- Used by GitWeb (a web-based Git viewer), but not required for regular use.
- HEAD (π)
- Points to the latest commit of the currently checked-out branch.
- Example: ref: refs/heads/main (means currently on main branch).
- index (π)
- Stores the staging area (aka "index"), tracking whatβs staged for commit.
- packed-refs (π)
- If Git optimizes refs, this file consolidates them instead of storing them in separate files.
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP

Member discussion