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

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

·

4 min read

Welcome to my Blog. Today we will dig deep into some of the advanced concepts for Git and GitHub and perform the tasks of the given day.

Git Stash

Git stash is a command in Git that allows you to temporarily save changes you've 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 don't want to commit the changes you've made in your current branch yet. Here's an example of how to use git stash:

  1. Run git stash to save your changes.

  2. Switch to the other branch with git checkout.

  3. Make your changes on the bug fix branch.

  4. Switch back to the feature branch with git checkout.

  5. Retrieve your stashed changes with git stash pop or git stash apply.

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. The 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.

Cherry-Pick

Git cherry-pick is a command in Git 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. Here's how to use git cherry-pick:

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

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

For example, let's say you have two branches, feature and bugfix. You made some changes to the feature branch that you want to apply to the bugfix branch. Here's how to do it:

  1. Switch to the bugfix branch with git checkout bugfix.

  2. Use git cherry-pick <commit_hash> to select the specific commit from the feature branch and apply it to the bugfix branch.

  3. Repeat step 2 for any other commits you want to apply.

This way, you can selectively apply changes from one branch to another without merging the entire branch.

Resolving Conflicts :

When multiple developers work on the same file, Git may detect a conflict when trying to merge their changes. Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. Git cannot automatically determine what is correct in such cases, so it marks the file as being conflicted and halts the merging process. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. To resolve conflicts, developers can use the following methods:

  1. Fetch and merge the latest changes from the remote repository with git pull.

  2. Identify the conflicting files with git status.

  3. Open the conflicting file using a text editor.

  4. Resolve the conflict by manually editing the file to combine the conflicting changes.

  5. Add or stage the changes with git add.

  6. Commit the changes with a comment using git commit.

Alternatively, developers can use merge tools or GUIs to resolve conflicts. There are two types of merge conflicts:

  1. Line conflict: When two developers change the same line of code in a file.

  2. Structural conflict: When the changes made by two developers affect the same file but do not conflict with each other line-by-line.

In summary, resolving conflicts in Git involves identifying the conflicting files, manually editing the files to combine the changes, and committing the changes.


Tasks

01)

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

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

    You won't be able to use git stash unless you track the file.To include the untracked files you need to modify the command git stash --include-untracked

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

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

02)

03)


Thankyou for reading till the end. Hope you've found something valuable.See you in the next blog.