🔄️Day 11 - Advance Git & GitHub for DevOps Engineers: Part-II

🔄️Day 11 - Advance Git & GitHub for DevOps Engineers: Part-II

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

Example of how git stash works:

Suppose you're working on a branch let's say feature in your Git repository and you've made changes to your files but you're not ready to commit them because you want to switch to another branch to fix a bug.

  1. To stash your changes

     git stash
    

    This will stash your changes, and gets your working directory clean.

  2. You can switch to another branch now using git checkout:

     git checkout <Next_Branch_name>
    
  3. After working on the other branch, you then decided to come back to your previous changes. To retrieve your changes from the stash, you can use:

     git stash pop
    

    This is a basic example of how git stash can be used to temporarily store changes in Git. It's very flexible when you need to switch work.

🌱Git 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.

Example:

  1. Create 2 Branches

      git checkout -b devops
      # Make changes and commits on devops branch
      git commit -m "Commit 1 on devops"
      git commit -m "Commit 2 on devops"
    
      git checkout -b feature
      # Make changes and commits on feature branch
      git commit -m "Commit 1 on feature"
      git commit -m "Commit 2 on feature"
    
  2. Now, apply "Commit 2 of devops branch" to feature branch

    find the commit hash code of "Commit 2 on devops":

     git log --oneline 
     #log command show all the commits with hash ID
    
  3. Switch to feature branch now:

      git checkout feature
    
  4. Cherry-pick the commit from devops branch:

      git cherry-pick <commit_hash_ID>
    
  5. Resolve conflicts You'll need to manually resolve these conflicts, stage the changes, and continue the cherry-pick with git cherry-pick --continue.

  6. Once conflicts are resolved (if any), the commit from devops will be cherry-picked onto feature.

🌱Conclusion

In summary, git stash is for temporarily storing changes at backstage, while git cherry-pick is for selectively applying specific commits to another branch. Both commands are valuable tools in managing your Git workflow efficiently.

Happy Learning😊

Did you find this article valuable?

Support Vivek's Blog by becoming a sponsor. Any amount is appreciated!