Advance Git & GitHub for DevOps Engineers: Part-2

Advance Git & GitHub for DevOps Engineers: Part-2

Git is a powerful version control system that allows developers to manage and track changes in their codebase. In this article, we'll explore some essential Git commands, including git stash, git cherry-pick, and handling conflicts during merges and rebases.

Git Stash

Git stash is a versatile command that enables developers to temporarily save changes made in their working directory without committing them. This is particularly useful when you need to switch to a different branch to work on something else but don't want to commit the changes in your current branch. Let's dive into how to use Git stash:

Task-01

  1. Create a new branch and make some changes: Start by creating a new branch and making the necessary modifications to your code.

  2. Use git stash to save the changes without committing them: After making changes, you can use the git stash command to save those changes. This command effectively takes the changes out of your working directory and records them in a new stash. You can verify the stashes with the git stash list command.

  3. Switch to a different branch, make changes, and commit them: Now, switch to a different branch and make additional changes. These changes will not be affected by the stashed changes.

  4. Use git stash pop to bring the changes back: To apply the stashed changes on top of your new commits, use the git stash pop command. This will merge the stashed changes with your current working directory.

Cherry-pick

Git cherry-pick is a valuable command that allows you to select specific commits from one branch and apply them to another. This is especially helpful when you want to pick and choose changes made in one branch and apply them to another. Let's see how it works:

Task-02

  1. Make changes in development branch: In your development branch, open the version01.txt file and add the desired lines.

  2. Commit the changes: After making the changes, commit them with appropriate commit messages.

  3. Reflect the changes in the Production branch: To reflect these commits in the Production branch, you can use the rebase command. By rebasing the Production branch onto the Development branch, you will include the new commits in the Production branch. These commits will be reflected with their original commit messages.

Resolving Conflicts

Conflicts can occur when you attempt to merge or rebase branches that have diverged. When conflicts arise, you need to manually resolve them before Git can proceed with the merge or rebase. Here's how to handle conflicts:

Task-03

  1. Cherry-pick Commit "Added feature2.2 in development branch" into Production branch: Use git cherry-pick to pick the desired commit from the Development branch and apply it to the Production branch.

  2. Add lines to the commit: Open the commit message where you cherry-picked and make the necessary additions. Then, commit the changes with an appropriate message, such as "Optimized the feature."

By following these tasks, you can effectively use Git stash, cherry-pick, and handle conflicts to manage your codebase, collaborate with your team, and maintain a clean and organized version control system. These Git commands are essential tools in a developer's toolkit and can help streamline your workflow and ensure your project's success.

HAPPY LEARNING...