Advance Git & GitHub for DevOps Engineers: Part-2

🔹Git Stash:

Git stash is a command that allows you to temporarily save changes you have made in your working directory, without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.

To use Git stash, you first create a new branch and make some changes to it. Then you can 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. You can apply these changes later. git stash list command shows the list of stashed changes.

You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.

Example: Let's say you are working on a feature branch in your Git repository and have made some changes to the files, but you are not yet ready to commit them. Suddenly, you need to switch to another branch to fix a critical bug.

Check the status of your working directory using git status:

git status

Stash your changes using git stash:

git stash

Now, switch to the branch where you need to fix the bug, e.g., bug-fix-branch:

git checkout dev

Make the necessary changes and commit them.

Once the bug is fixed, switch back to your original branch (e.g., feature branch) where you stashed your changes:

git checkout main

Make the necessary changes and commit them.

Once the bug is fixed, switch back to your original branch (e.g., feature branch) where you stashed your changes:

git stash apply

Now, your local changes from the feature branch are restored, and you can continue working on them. Remember, if you want to remove the stashed changes, you can use git stash drop. If you want to apply and remove the stash in one command, you can use git stash pop.

Git stash is a handy feature that allows developers to work efficiently and switch between tasks without the need for unnecessary commits.

🔹Cherry-pick:

Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.

To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.

Example: Let's say you have two branches in your Git repository: dev and master. A bug was fixed in the dev, and you want to apply that fix to the main branch.

First, ensure you are on the branch where you want to apply the commit (in this case, the master branch):

git checkout main

Now, identify the commit hash or unique identifier of the specific commit you want to cherry-pick. You can find this by using git log:

git log

Once you have the commit hash, use the git cherry-pick command followed by the commit hash:

git cherry-pick <commit-hash>

🔹Resolving Conflicts:

Merging conflicts occur when two or more developers make conflicting changes to the same part of a file in different branches. Git cannot automatically resolve these conflicts, and manual intervention is required to determine which changes to keep or how to combine them. Developers must carefully review the conflicting sections, edit the file to remove conflict markers, and then stage and commit the resolved changes to complete the merge. Resolving conflicts is essential for maintaining code quality and promoting collaboration in a shared codebase.

  1. Decide which version of the conflicting changes you want to keep. You have two options:

    • "Accept Current Change": This means you want to keep the changes from your current branch and discard the incoming changes.

    • "Accept Incoming Change": This means you want to keep the changes from the incoming branch and discard the current changes.

  2. Edit the File: Manually edit the file to keep the changes you want. Remove the conflict markers and any unnecessary lines. Make sure the resulting code is correct and functions as expected.

  3. Save the Changes: