Mastering Git and GitHub for DevOps Engineers: Part 2

In our ongoing journey of understanding Git and GitHub for DevOps, today we explore some advanced concepts that will empower you in your development and collaboration efforts. Let's dive into the theoretical definitions:

Git Stash

Imagine you're working on a new feature or bug fix in your current branch, and suddenly, you need to switch to a different branch for an urgent task. You're not ready to commit your current changes, but you can't take them with you to the new branch as-is. This is where Git stash comes to the rescue.

Git stash is a command that allows you to temporarily save changes you've made in your working directory without committing them. It's like putting your changes in a hidden drawer. Here's how it works:

  1. Create a new branch and make some changes.

  2. Use git stash to save those changes. This action removes the changes from your working directory and records them in a new stash. You can use git stash list to see your stashed changes.
    When you're ready, you can apply these stashed changes back to your branch. You can also use git stash drop to delete a specific stash or git stash clear to delete all stashes.

Cherry-pick

In your Git journey, you might encounter situations where you need to select specific commits from one branch and apply them to another. This is where Git cherry-pick shines. It allows you to pick cherries (commits) selectively and transplant them to another branch.

Here's how it works:

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

  2. Use git cherry-pick <commit_hash> to select specific commits from one branch and apply them to the other.
    Cherry-pick is like handpicking the ripe commits from one branch and planting them in another.

Resolving Conflicts

Conflicts are common in Git when you're merging or rebasing branches that have diverged. These conflicts need to be resolved manually before Git can proceed with the merge or rebase. Here are some essential commands:

  • git status: This command shows you which files have conflicts.
    git diff: It reveals the differences between the conflicting versions.
    git add: Use this command to stage the resolved files once you've manually resolved the conflicts.
    Now, let's put this knowledge into practice:

Task 1:

  1. Create a new branch and make changes in it.

  2. Use git stash to save these changes without committing them.
    Switch to a different branch, make changes, and commit them.
    Use git stash pop to bring back the stashed changes and apply them on top of the new commits.

Task 2:

Add new content to version01.txt in the development branch and commit as instructed. Make sure these commit messages are also reflected in the Production branch, which originates from the Master branch. You can achieve this by using rebase.

Task 3:

In the Production branch, cherry-pick a specific commit from the development branch and add new lines to it. Commit these changes.

0
Subscribe to my newsletter

Read articles from Aakanksha Deshmukh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aakanksha Deshmukh
Aakanksha Deshmukh

I am a Devops Engineer