#90DaysOfDevOps Challenge - Day 10 - Advanced Git & GitHub for DevOps Engineers Part 1
Welcome to Day 10 of the #90DaysOfDevOps challenge. Today, we will explore advanced Git techniques, including branching, merging, and reverting. These techniques are essential for effective collaboration and version control in software development projects. So, let's dive in.
Git Branching
Git branching is a feature in Git that allows you to create separate lines of development within a repository. It enables you to work on different features or fixes simultaneously without affecting the main codebase.
Branches serve as independent workspaces where you can make changes, commit them, and merge them back into the main branch when ready. They provide a way to organize and manage different versions or streams of code within a project.
Git Revert and Reset
Git revert
is a command that undoes a specific commit by creating a new commit that undoes the changes made in that commit. It is a safe way to undo changes without altering the commit history.
Git reset
is a command that allows you to move the branch pointer to a different commit. It can be used to reset the branch to a previous state. However, it should be used with caution as it can discard or modify changes in the process.
In simple terms, git revert
undoes a commit by creating a new one, while git reset
moves the branch pointer to a different commit.
Git Rebase and Merge
What Is Git Rebase?
Git rebase
is a command that allows you to update your branch with the latest changes from another branch. It rearranges the commit history by moving your changes on top of the updated branch. This helps create a cleaner and more straightforward history of your changes. It's useful for integrating changes, keeping your branch up to date, and making your commit history more organized before merging.
What Is Git Merge?
Git merge
is a command that combines changes from different branches into a single branch. It takes the changes made in one branch and integrates them into another branch, creating a new commit that includes the changes from both branches. It's used to incorporate the work done in one branch into another, such as merging a feature branch into a main branch. The merge operation keeps a record of the individual branch histories and combines them into a unified branch history.
Refer to this article for a better understanding of Git Rebase and Merge Read here
Task 1: Branching, Committing, and Restoring
In this task, we will demonstrate how to create a branch, add commits with different messages, and restore a file to a previous version. Follow the steps below:
- Create a branch called
dev
from themain
branch using the command:
git branch dev
git checkout dev
Add a text file called
version01.txt
inside theDevops/Git/
directory. Write the following content inside the file:"This is the first feature of our application."
Commit this change with the message "Added new feature."
- Push the
dev
branch to the remote repository using the command:
git push origin dev
Add new commits to the
dev
branch by modifying theversion01.txt
file with the following content:This is the bug fix in the development branch
Commit this change with the message
git commit -m "Added feature2 in the development branch."
Repeat this step two more times, adding the following content and committing with appropriate messages:
This is gadbad code
This feature will gadbad everything from now.
Restore the
version01.txt
file to a previous version where the content should be "This is the bug fix in the development branch."Using the
git log --oneline
command, we can find the<commit>
information and identify the commit you want to reset to.We can use the
git reset <commit>
command to remove the last two commits where we added the second and third lines and move the changes to the unstaged area.
Task 2: Branching, Merging, and Rebasing
In this task, we will demonstrate the concept of branches, merging, and rebasing. Follow the steps below:
Create two or more branches with different names using the command
git branch
. In this case, I will usemain
anddev
:git branch dev
Make some changes to the
dev
branch and commit them. Take a screenshot of the commit history and branch visualization to demonstrate the concept of branches.Merge the
dev
branch into themain
branch using the command:git checkout main git merge dev
As a practice, perform a
git rebase
operation to see the difference it makes. Describe the differences you observe.
git rebase dev
Merge preserves the branch structure and creates a new merge commit, while rebase rewrites the commit history and provides a linear sequence of commits. The choice between merge and rebase depends on the specific use case, project requirements, and collaboration workflow.
Completed Day 10 of the #90DaysOfDevOps challenge. Today, we explored advanced Git techniques, including branching, merging, and reverting. These techniques play a crucial role in efficient collaboration and version control in software development projects.