Advance Git & GitHub for DevOps Engineers: Part-2

ยท

4 min read

Welcome to Part-2 of our series on "Advance Git & GitHub for DevOps Engineers." In this installment, we'll delve into more powerful Git commands and techniques, including Git stash, cherry-pick, and resolving conflicts. Let's explore these essential features and how they can enhance your Git workflow.

  1. Git Stash: Saving Changes Temporarily ๐Ÿ“ฆ

Git stash is a lifesaver when you need to temporarily save changes you've made in your working directory, without committing them. This is particularly useful when you need to switch to a different branch to work on something else but want to preserve your current changes.

How to use Git stash:

  1. Create a new branch and make some changes to it.

  2. Use the command git stash to save those changes. This will remove the changes from your working directory and record them in a new stash.

  3. You can apply these changes later using git stash pop to bring the changes back and apply them on top of the new commits.

Commands for managing stashes:

  • git stash list: Shows the list of stashed changes.

  • git stash drop: Deletes a specific stash.

  • git stash clear: Deletes all stashes.

  1. Cherry-pick: Selectively Applying Commits ๐Ÿ’

Git cherry-pick allows you to choose specific commits from one branch and apply them to another. This is valuable when you want to selectively apply changes made in one branch to another.

How to use Git cherry-pick:

  1. Create two new branches and make some commits to each of them.

  2. Use git cherry-pick <commit_hash> to select specific commits from one branch and apply them to the other.

  3. Resolving Conflicts: Tackling Diverged Branches ๐Ÿ”„๐Ÿ”ง

Conflicts can arise when merging or rebasing branches that have diverged, and you need to manually resolve them before proceeding with the merge/rebase.

Resolving Conflicts:

  1. Use git status to identify the files that have conflicts.

  2. Utilize git diff to view the difference between the conflicting versions.

  3. Manually edit the files to resolve the conflicts.

  4. Use git add to add the resolved files to the staging area.

  5. Finally, complete the merge/rebase process with git commit.

Task-01: Saving and Applying Changes with Git Stash ๐Ÿ“‹

  1. Create a new branch and make some changes to it.

  2. Use git stash to save the changes without committing them.

  3. Switch to a different branch, make some changes, and commit them.

  4. Use git stash pop to bring the changes back and apply them on top of the new commits.

Task-02: Feature Integration using Cherry-pick ๐Ÿ’

  1. In version01.txt of the development branch, add the following lines after "This is the bug fix in the development branch" that you added in Day10 and then reverted to that commit:

     Line2>> After bug fixing, this is the new feature with minor alteration
    

    Commit this with the message "Added feature2.1 in the development branch."

  2. Add the following line after "This is the advancement of the previous feature":

     Line3>> This is the advancement of the previous feature
    

    Commit this with the message "Added feature2.2 in the development branch."

  3. Add the following line:

     Line4>> Feature 2 is completed and ready for release
    

    Commit this with the message "Feature2 completed."

Ensure that all these commit messages are reflected in the Production branch, which is derived from the Master branch (Hint: try rebase).

Task-03: Applying Specific Changes to Production Branch ๐Ÿš€

In the Production branch, cherry-pick the commit "Added feature2.2 in the development branch" and add the following lines to it:

Line to be added after Line3>> This is the advancement of the previous feature
Line4>> Added few more changes to make it more optimized.

Commit this with the message "Optimized the feature."

Conclusion:

In this part of our series, we explored Git stash, cherry-pick, and resolving conflicts, essential tools for DevOps Engineers. By mastering these advanced Git commands, you can efficiently manage changes, integrate features, and tackle conflicts with ease. Stay tuned for Part-3, where we'll delve into even more powerful Git techniques to enhance your development workflow. Happy coding! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

ย