Advance Git & GitHub for DevOps Engineers
🔀Git Branching:
Git branching is a powerful feature of the Git version control system that allows developers to create separate lines of development, known as branches, from the main codebase.
Each branch operates independently, enabling developers to work on different features, bug fixes, or experiments without interfering with the main codebase or other branches.
The main branch, often called the "master" branch or the "main" branch, represents the stable and production-ready version of the code. When a new feature or bug fix is needed, developers can create a new branch from the main branch. This branch becomes a copy of the main branch, and any changes made to it will be isolated from the main branch. Developers can switch between branches to work on different tasks and make commits specific to each branch.
Simple Example:
Imagine you are working on a project using Git, and you want to add a new feature to your codebase. Instead of making changes directly to the main branch, you decide to create a separate branch for your new feature to keep your work isolated until it's ready to be merged into the main codebase. This separate branch is called a feature branch.
Let's go through the steps of creating a feature branch, making changes, and eventually merging it back into the main branch using Git:
Create a New Branch:
git checkout -b feature/add-new-feature
In this example, you create a new branch named "feature/add-new-feature" using the git checkout -b
command. The -b
flag indicates that you want to create a new branch.
Make Changes:
Now that you are on the "feature/add-new-feature" branch, you start making changes to your codebase to implement the new feature. You make multiple commits as you progress, using git add
to stage changes and git commit
to create commits.
git add file1.js file2.js
git commit -m "Implemented feature A"
git add file3.js
git commit -m "Added feature B"
Switch Back to Main Branch:
Once you have completed the work on your feature branch and tested it thoroughly, it's time to switch back to the main branch to integrate your changes.
git checkout main
🤝🔀Git Merge and Rebase
Git Merge:
Git merge is a command in Git that allows developers to combine changes from one branch into another. It's a way to integrate the changes made in a source branch into a target branch, resulting in a new "merge commit" that combines the changes from both branches.
Example:
In our example of working with a feature branch and the main branch, let's explore how Git merge fits into the picture. After completing the work on the "feature/add-new-feature" branch, you are ready to integrate those changes into the main branch. To do this, you use Git merge:
# Switch to the main branch
git checkout main
# Merge the changes from the feature branch into the main branch
git merge feature/add-new-feature
Git merge takes the changes made in the "feature/add-new-feature" branch and combines them with the main branch. If there are no conflicts, Git will create a new merge commit that represents the combination of both branches' changes.
The benefit of Git merge is that it retains the complete commit history of both branches, creating a clear record of the development process.
Git Rebase:
Git rebase is a powerful Git command used to modify the commit history of a branch. It allows developers to integrate changes from one branch into another by moving, combining, or omitting commits. Unlike Git merge, which creates a new "merge commit" to combine changes, Git rebase applies individual commits directly onto the target branch, resulting in a cleaner and more linear commit history.
Example:
Now, let's explore how Git rebase comes into play. Suppose you're working on a separate feature branch called "feature/add-another-feature." After some time, you want to update your branch with the latest changes from the main branch before merging your work:
# Switch to the feature branch
git checkout feature/add-another-feature
# Rebase the feature branch onto the main branch
git rebase main
Git rebase takes the changes made in the "feature/add-another-feature" branch and "replays" them on top of the latest state of the main branch. It essentially moves your feature branch to the tip of the main branch, creating a cleaner and more linear commit history.
The typical use case for git rebase is when developers want to update their feature branch with the latest changes from the main branch (often called the "upstream" branch). By rebasing their branch onto the main branch, developers can make it appear as if they developed their changes directly on top of the latest codebase, even if other commits have been made to the main branch in the meantime.
⏮️🔄Git Revert and Reset:
Git Revert:
When a mistake is made or an undesired change is committed, Git Revert comes to the rescue. It enables developers to gracefully roll back changes without affecting the rest of the commit history or the work of other team members. Git Revert is a safer option for undoing commits when collaborating with others on a shared repository.
Example:
Let's say that after merging the "feature/add-new-feature" branch into the main branch, you realize that there is a bug in one of the changes you made on the feature branch. Instead of using Git reset, which modifies the commit history, you decide to use Git revert to undo the specific commit that introduced the bug.
# Identify the commit hash of the problematic commit (for example: abcdefg)
git log
# Revert the problematic commit
git revert abcdefg
Git revert will create a new commit that undoes the changes introduced by the problematic commit. It effectively removes the bug-causing changes from the main branch without altering the commit history.
Git Reset:
Git Reset is another powerful command that allows developers to move the HEAD (the current position) to a specified commit, effectively resetting the codebase to a previous state. Unlike Git Revert, which creates an inverse commit, Git Reset modifies the commit history by "unstaging" or "discarding" commits.
Example:
Let's say you decided to use Git reset on the "feature/add-new-feature" branch to undo the last commit because you realized that the changes were not needed after all.
# Identify the commit hash of the last commit on the feature branch (for example: xyz123)
git log
# Reset the branch to the previous commit, removing the last commit
git reset --hard xyz123
Git reset with the --hard
option resets the branch to the specified commit, effectively discarding the last commit and all its changes. However, be cautious with Git reset --hard as it permanently removes the commit and its changes from the branch. If you've already pushed the changes to a remote repository and others have pulled those changes, using Git reset can lead to complications and conflicts for other developers.
In summary, Git revert is a safer option when you want to undo specific commits without altering the commit history. It creates a new commit that undoes the changes introduced by the specified commit. On the other hand, Git reset is more powerful but potentially riskier, as it permanently discards commits and changes from the branch, rewriting the commit history.
🚀 Task: Demonstrate the concept of Branching, Committing, Pushing to Remote & Merging
Create a new branch named "dev" from the main branch.
git checkout -b dev
Create a new text file named version01.txt and fill it with the following content:
touch version1.txt
Use the "git add" command to add the file to the staging area
git add <filename>
Use "git commit" to commit the changes
Push the
dev
branch to the remote repositorygit push origin dev
- Let's merge the "dev" branch with the "main" branch.
Execute the below command to combine the commits of "main" and "dev" branches.
git merge dev
🌟Conclusion:
In this comprehensive blog, we embarked on an exciting journey through the world of Git, discovering its essential features and functionalities. From understanding Git Branching, which empowers us to create parallel universes for developing new features, to mastering the art of Rewind with Git Revert and Reset, we have unlocked the potential of version control.
We explored Git Rebase and Merge, two powerful methods for uniting the forces of collaboration and seamlessly integrating changes from one branch into another. These techniques enable us to keep a clean and organized commit history, making our projects more manageable and transparent.
Through practical examples and real-world scenarios, we've witnessed the beauty of Git in action. Its distributed nature, ease of branching, and powerful version control capabilities have revolutionized the way developers work and collaborate on projects.
So, let's embrace Git, collaborate seamlessly, and code with confidence on our journey to building remarkable software! 🚀💻🌟