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

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

ยท

2 min read

๐Ÿ”ท Git Stash:

Git stash serves as a command enabling the temporary preservation of changes made in the working directory without committing them. This proves beneficial when necessitating a switch to a different branch for alternate work, while deferring the commitment of current branch changes.

To utilize Git stash, start by creating a new branch and implementing modifications. Next, execute the command 'git stash' to save these alterations. This action removes the changes from the working directory, cataloging them in a new stash. Subsequently, these stashed changes can be applied later. Employ 'git stash list' to display the catalog of stashed changes. Additionally, 'git stash drop' allows the deletion of a specific stash, while 'git stash clear' deletes all stashes.

๐Ÿ”ท Cherry-pick:

Git cherry-pick is a command offering the selection of specific commits from one branch for application to another. This feature proves valuable in selectively applying alterations made in one branch to another.

To leverage git cherry-pick, create two new branches and perform commits in each. Then, utilize 'git cherry-pick <commit_hash>' to select and apply specific commits from one branch to the other.

๐Ÿ”ท Resolving Conflicts:

Conflicts may arise during branch merges or rebases when branches diverge, necessitating manual resolution before git can continue. Utilize 'git status' to display files containing conflicts. Employ 'git diff' to view discrepancies between conflicting versions, while 'git add' aids in adding the resolved files.

๐Ÿ›  Practice Example for Git Stash:

๐Ÿ”ท Task Instructions:

  1. Create a New Branch and Apply Changes:

    ๐Ÿ”ธ Begin by creating a new branch in your Git repository.

    ๐Ÿ”ธ Implement alterations or additions within this new branch.

  2. Save Changes Without Committing Using Git Stash:

    ๐Ÿ”ธ Utilize 'git stash' to preserve the changes made in the current working directory without committing them.

  3. Switch to Another Branch and Commit Changes:

    ๐Ÿ”ธ Shift to a different branch in your repository.

    ๐Ÿ”ธ Make new modifications or additions within this branch.

    ๐Ÿ”ธ Commit these changes using git commit.

  4. Apply Stashed Changes to the Current Branch:

    ๐Ÿ”ธ Apply the stashed changes onto the present branch and on top of the recent commits.

    ๐Ÿ”ธ Use git stash pop to retrieve the saved changes and incorporate them into the current branch.

ย