Day 13 : Advance Git & GitHub for DevOps Engineers
Git Branching
Branches are an essential feature in Git, allowing you to work on different project parts without affecting the main codebase. Each Git repository starts with a default branch (usually called main
or master
). You can create new branches for different tasks like adding new features, fixing bugs, or experimenting with changes. This keeps your main project safe from unfinished or buggy code.
Creating a Branch
To create a new branch, use the git branch
command. For example, to create a branch named feature
, run:
#Creating a Branch
git branch feature
#Switching to a Branch
git checkout feature
Creating and Switching in One Step
You can create and switch to a new branch in one step with the -b
flag:
git checkout -b feature
Merging a Branch
When your work on a branch is done, you can merge it back into the main branch. First, switch to the main branch:
git checkout master
Then merge your branch:
git merge feature
Branches help organize your work, making it easier to manage changes and collaborate with others.
Git Revert and Reset
Both git revert
and git reset
undo changes, but they work differently:
Git Reset
git reset
moves the HEAD
pointer to a specific commit, changing the commit history. It has three modes:
--soft
: Keeps changes in the working directory and staging area.--mixed
: Keeps changes in the working directory but clears the staging area.--hard
: Discards all changes, both in the working directory and the staging area.
Example of a hard reset:
git reset --hard <commit_hash>
Git Revert
git revert
creates a new commit that undoes the changes of a specific commit, preserving the commit history. It’s safer for teamwork since it doesn’t rewrite history.
Example:
git revert <commit_hash>
Git Rebase and Merge
Both git rebase
and git merge
integrate changes from one branch into another, but they do it differently:
Git Rebase
git rebase
rewrites the commit history by moving the entire branch to a new base commit, creating a linear history.
git checkout feature
git rebase master
This command applies the changes from the feature
branch on top of the master
branch, making the history linear.
Git Merge
git merge
combines changes from one branch into another by creating a new merge commit, preserving the commit history of both branches.
git checkout master
git merge feature
While rebase
results in a cleaner history, merge
is better for preserving the context of feature development.
Tasks
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
Create a text file called
version01.txt
inside theDevops/Git/
directory with the content “This is the first feature of our application”.Create a new branch from
master
:git checkout -b dev
Add and commit the changes:
git add Devops/Git/version01.txt git commit -m "Added new feature"
Push Changes to GitHub:
Push the local commits to the repository on GitHub:
git push origin dev
Add More Features with Separate Commits:
Update
version01.txt
with new lines and commit after each change:echo "This is the bug fix in development branch" >> Devops/Git/version01.txt #stage new file (version01.txt) with git add git add version01.txt git commit -am "Added feature2 in development branch" echo "This is gadbad code" >> Devops/Git/version01.txt #use -am to stages and commits all modified files git commit -am "Added feature3 in development branch" echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt git commit -am "Added feature4 in development branch"
Restore the File to a Previous Version:
Use
git revert
to undo the last two commits:git revert HEAD~2
Task 2: Working with Branches
Demonstrate Branches:
Create multiple branches:
git branch feature1 git branch feature2
To see all local and remote branches, which helps you understand the complete branch structure of your repository.
git branch -a
Merge Changes into Master:
Make changes to the
dev
branch and merge intomaster
:git checkout dev echo "More changes" >> Devops/Git/version01.txt git commit -am "Added more changes in dev branch" git checkout master git merge dev
Practice Rebase:
Try rebasing
dev
ontomaster
and observe the changes:git checkout dev git rebase master
Mastering Git and GitHub techniques is essential for effective DevOps practices. Using branches reverts, resets, rebases, and merges helps you manage code changes efficiently, collaborate seamlessly, and keep your project history clean and organized. These skills are vital for any DevOps engineer aiming to streamline development workflows and ensure code quality.
Subscribe to my newsletter
Read articles from vinod chandra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by