Day 11 : Advance Git & GitHub for DevOps Engineers: Part-2

Day 11 : Advance Git & GitHub for DevOps Engineers: Part-2

Git Stash:

The meaning of stash is "store something safely in a hidden place." The sense in Git is also the same for stash. Git temporarily saves your data safely without committing.

It takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.

The git stash command saves the previously written code and then returns to the last commit for a fresh start.

Why do we use Git stash?

git stash as a practical way to set aside your work-in-progress changes temporarily, so you can deal with other tasks without losing your progress. It is like putting your work on hold without losing track of it.

Commands we use in git stash are:

  • Git stash: This command is used to stash away your current changes in a safe place, leaving your working directory clean. It's a quick way to temporarily set aside your changes without committing them.

  • Git stash save "message": This is an older syntax for git stash. It allows you to stash changes with an optional descriptive message.

  • Git stash list: This command lists all stashed changes. It shows a numbered list of stashes along with a message for each stash if provided.

  • Git stash apply: This command retrieves the most recent stash and applies it to your working directory, reapplying the changes you stashed. The changes remain in the stash after applying.

  • Git stash changes: This command shows the changes in the most recent stash. It's useful for previewing what changes are stored in the stash.

  • Git stash pop: This command retrieves the most recent stash, applies the changes to your working directory, and removes it from the stash list.

  • Git stash drop: This command discards the most recent stash without applying its changes to your working directory. It's like throwing away the stash.

  • Git stash clear: This command removes all stashes. Use it with caution as it removes all stashes permanently.

What is Git-cherry pick?

Git cherry-pick is a command that allows us to pick a commit from one branch and apply it to another branch.

This can be useful when you want to bring in changes from one branch to another without merging the entire branch.

Git Cherry-pick

The syntax for cherry-pick is: git cherry-pick <commit-hash>

In this syntax, the <commit-hash> is like the unique identifier of the commit we want to use. When we use git cherry-pick, Git takes all the changes from that commit and copies them onto the branch we're working on. So, it's like grabbing specific changes from one place and putting them into another.

Why do we use cherry-pick?

Imagine you're in a group working on a big project. Someone suggests some changes, but you only want to add a few to the main project, not everything. Instead of taking all their work, you just pick the bits you like and add them to the main project. This way, things stay organized and you only add what you need. That's what cherry-picking is all about.

Some important use cases of cherry-pick:

  • It is a helpful tool for teams working together.

  • It is mainly used for reversing changes and getting back lost updates.

  • It is important when fixing bugs because fixes are made and tested in the development branch with their changes.

  • We can prevent unnecessary conflicts by using git cherry-pick instead of other methods.

  • It is a useful tool when you can't merge a whole branch because the versions in different branches don't match.

  • Git cherry-pick lets you grab changes from a sub-branch without switching branches.

Disadvantages of using Cherry Pick

Cherry-picking isn't always the best option because it can create duplicate commits and cause issues where regular merges are better. Also, if changes from different branches touch the same lines of code and you try to cherry-pick one change into another branch, it can lead to conflicts.

Conflicts:

Git merge conflict tutorial - Ihatetomatoes

What is git merge conflict?

Git Merge and Merge Conflict

A git merge conflict happens when Git can't automatically combine changes from two different commits. It can handle the merge automatically only when the changes are in different places or branches.

How does conflict arise?

Suppose there are two users and both are working on the same file and updating the same line of code in two different ways and committing. In this case, conflict arises and Git is unable to configure which change it should accept.

How to resolve conflicts?
Here are some steps that could simplify the process of resolving merge conflicts in Git:

Step 1: The simplest way to handle a conflicted file is to open it and make the required adjustments.

Step 2: Once you've edited the file, you can use the git add command to stage the newly merged content.

Step 3: Finally, create a new commit using the git commit command.

Step 4: Git will generate a new merge commit to complete the merge.

Summary

In this blog, we see the Git concepts including Git Stash, Git Cherry-pick, and Git Merge Conflicts. Git Stash is introduced as a tool for temporarily saving changes without committing them, with detailed explanations of commands like stash, save, apply, and drop. Git Cherry-pick is discussed as a method for selectively applying specific commits from one branch to another, with examples illustrating its utility in team collaboration and code management. Additionally, it explores the concept of Git Merge Conflicts, explaining why conflicts occur and offering step-by-step guidance on resolving them. Overall, the blog serves as a valuable resource for developers seeking to enhance their understanding and proficiency in using Git for version control.