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 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
mainbranch using the command:
git branch dev git checkout dev
Add a text file called
Devops/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."
devbranch to the remote repository using the command:
git push origin dev
Add new commits to the
devbranch by modifying the
version01.txtfile with the following content:
This is the bug fix in the development branch
Commit this change with the message
"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.
version01.txtfile to a previous version where the content should be "This is the bug fix in the development branch."
git log --onelinecommand, 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 use
git branch dev
Make some changes to the
devbranch and commit them. Take a screenshot of the commit history and branch visualization to demonstrate the concept of branches.
devbranch into the
mainbranch using the command:
git checkout main git merge dev
As a practice, perform a
git rebaseoperation to see the difference it makes. Describe the differences you observe.
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.
Congratulations on completing 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.
Stay tuned for Day 11, where we will continue exploring Git and GitHub for DevOps Engineers in the second part of this topic.
Did you find this article valuable?
Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!