Git Branching
Branching is a fundamental concept in Git that allows you to work on new features or fixes independently from the main codebase. Each project starts with a default branch (usually main
or master
), and you can create as many additional branches as needed. Once your work is ready, you can merge your branch into another using a pull request.
Branches help you safely test changes, build features, and fix issues without disrupting the main project.
Git Reset vs Revert
Both git reset
and git revert
help you undo changes in your code, but they serve different purposes:
-
git reset
moves the pointer of your branch to a previous state and can modify your commit history (best used in local branches). -
git revert
creates a new commit that undoes the changes from a previous commit, preserving history (ideal for shared branches).
Git Rebase vs Merge
Both commands are used to integrate changes from one branch into another, but they do it differently:
- Git Rebase rewrites commit history to make it linear, which is helpful for a clean and organized project history.
- Git Merge combines the changes while preserving the history of both branches. This is useful when you want a clear picture of how changes were made over time.
Each method has its advantages depending on your workflow and team preferences.
Task 1: Feature Development with Branches
✅ Step-by-Step Instructions
Navigate to your local repository:
cd path/to/your/local/DevOps-repo
Create the directory (if not already created):
mkdir -p Devops/Git
Create the version01.txt
file and add content to it:
echo "This is the first feature of our application" > Devops/Git/version01.txt
Check your current branch (ensure you’re on master):
git branch
Create and switch to a new branch named dev
:
git checkout -b dev
Stage your new file for commit:
git add Devops/Git/version01.txt
Commit the changes with a message:
git commit -m "Added new feature"
Once done, you’re ready to push your changes to GitHub.
Task 2: Push Changes to GitHub:
Make sure you’re still on the dev
branch:
git branch
Output should show:
* dev
Push the dev
branch to your GitHub remote repository:
git push origin dev
✅ This command will create the
dev
branch on GitHub and upload your changes there.
After this, you should see the new branch appear on GitHub, and you’ll be able to create a pull request from it if needed.
Task 3: Add More Features with Separate Commits:
Ensure you’re still in the root of your Git repository and on the
dev
branch.
📌 1st Change:
Append the first new line to version01.txt
:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
Commit it:
git commit -am "Added feature2 in development branch"
📌 2nd Change:
Append the second new line:
echo "This is gadbad code" >> Devops/Git/version01.txt
Commit it:
git commit -am "Added feature3 in development branch"
📌 3rd Change:
Append the third new line:
echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
Commit it:
git commit -am "Added feature4 in development branch"
Task: Working with Branches
1. ✅ Check out to your main branch (usually main
or master
)
git checkout main
2. ✅ Create two new branches
git checkout -b feature-login
Make a small change or create a file:
echo "Login feature started" > login.txt
git add login.txt
git commit -m "Initial login feature"
Go back to main:
git checkout main
Now create another branch:
git checkout -b feature-logout
Make a change:
echo "Logout feature started" > logout.txt
git add logout.txt
git commit -m "Initial logout feature"
3. ✅ View the branch structure
You can use the following to visualize branch structure:
Option 1: Simple list of branches
git branch