ππ Advanced Git and GitHub for DevOps engineers.π₯π₯
π Introduction: π¨βπ«
Hey there! In this article, we're going to explore some super handy and flexible Git commands. By learning these advanced features, you'll be all set to manage complex branching strategies, sort out conflicts like a pro, and keep your commit history neat. So, let's jump in and discover commands that will make you the DevOps engineer that you have always wanted to be!
β Using git merge and rebase: βοΈ
When working with multiple branches in Git, it's crucial to incorporate all your changes into the main branch. For this, there are two commands available: rebase and merge. Both combine the work of multiple developers into a single entity, integrating changes between branches.
πΈ Git merge: β
While working on a new feature in the main branch, you decide to create a separate branch. After finishing working on the feature you want to merge your work into the main branch.
If you want to merge the main branch into the feature branch, first "checkout" to the main branch (i.e., the branch from which changes will be merged). Then, execute git merge feature, and the commits from the main branch will be merged into the feature branch. This will also create a new commit just for the merge. Also after merging, you can see the complete history of commit merging.
πΈ Git rebase: β«
Rebasing is the process of altering the base of your branch from one commit to another, giving the impression that you created your branch from a different commit. rebase is used when we want to achieve a linear git history.
Let's consider the following scenario: you create a new branch from the main branch to work on a new feature. As you develop the feature, you notice that the main branch has new commits. Now, you want to incorporate those changes into your branch. Here we can use the rebase command.
In the above image, we can see that after rebasing it seems that the feature branched was started from the point where the new commits have been added in the main.
You must never use rebase on a remote repository as it removes the history of commits making it hard to track the changes.
β Difference between rebase and merge: π
merge | rebase |
Merge lets you merge different Git branches. | Rebase allows you to integrate the changes from one branch into another. |
Merge logs show you the complete history of commit merging. | Rebase logs are linear. As the commits are rebased, the history is altered to reflect this. |
All the commits on a feature branch are combined into a single commit on the master branch. | All commits are rebased, and the same number of commits are added to the master branch. |
Merge is best used when the target branch is supposed to be shared. | Rebase is best used when the target branch is private. |
Merge preserves history. | Rebase rewrites history. |
βMerge conflicts: βοΈ
A merge conflict occurs when Git cannot automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on separate lines or branches.
In the following example, when merging two branches, namely master and dev, a conflict arises due to differing lines of code on the same line in both branches. While resolving commits in Visual Studio Code, an editor is provided where we can select which changes to incorporate in the final commit.
Here, I have combined both changes, prioritizing the current changes since I am currently checked out in the main branch. This way, the new feature is also added, but the hotfix and bug fixes that were completed in the master branch are all available in the master branch.
Here we can see this visually, now the master branch has the new feature code as well as the bug fix and hot fix made on the master branch.
β Git stash: πΎ
While working on a feature, if we want to test something new without losing our current work, we can use the stash command. This command saves our changes and lets us use them again later if we want to.
In the example below, we have made some modifications to the index.js file. However, if we want to make new changes with a clean slate, we can use the git stash command.
After executing git stash and checking the status, we'll see that the staged changes have disappeared. By examining the output of the git stash command, we can confirm that it has saved the working directory in the stash.
To retrieve our stashed changes, simply execute git stash pop
. This will take the most recent stash and place it in our working directory.
β Cherry Pick : π
Git cherry-pick is a powerful command that allows you to select specific Git commits by reference and append them to the current working HEAD. Cherry-picking involves choosing a commit from one branch and applying it to another.
Let's an example where there are two branches ie master and feature01-bugFix. The bug fix branch contains multiple commits.
*master branch
*feature01-bugFix branch
Now we can use the cherry-pick command to get all those commits into the master branch.
After executing the cherry-pick command we will get a merge conflict as the master branch has some work that feature01-bugFix does not have.
Git cherry-pick is a useful tool, but it's not always the best practice. Cherry-picking can lead to duplicate commits, and in many situations where cherry-picking would be effective, traditional merges are preferred.
π Conclusion:
Hey there! You've now become a Git whiz by learning awesome commands like merge, rebase, stash, and cherry-picking. Keep practicing them and you'll soon feel super confident using them in real-life situations. Happy learning!
Subscribe to my newsletter
Read articles from Yashraj Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Yashraj Jaiswal
Yashraj Jaiswal
Hey there! π I'm Yashraj Jaiswal, a friendly web developer who's super excited about diving into the world of DevOps. I love coding and can't wait to learn more every day. As I go on this adventure, I'll be sharing my thoughts and discoveries about DevOps on my Hashnode blog. So, come along and let's explore the amazing world of web development and DevOps together! Don't forget to connect and share your own experiences too! π