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

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

Git Stash

Git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but your mid-way through a code change and aren’t quite ready to commit.

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. 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.

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

How to use git stash

Here's the sequence to follow when using git stash:
1. Save changes to branch A.
2. Run git stash.
3. Check out branch B.
4. Fix the bug in branch B.
5. Commit and (optionally) push to remote.
6. Check out branch A
7. Run git stash pop to get your stashed changes back.

Cherry-pick

Git cherry-pick command allows you to integrate selected, individual commits from any branch into your current HEAD branch. This is in contrast with other ways such as merge and rebases which normally apply many commits into another branch.

Cherry-picking is just like rebasing, an advanced concept and also a powerful command. It is mainly used if you don’t want to merge the whole branch and you want some of the commits.

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.

Resolving Conflicts

Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with the merge/rebase. git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files.

Task-01

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

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

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

Task-02

  • In version01.txt of development branch add below lines after “This is the bug fix in development branch” that you added in Day10 and reverted to this commit.

  • Line2>> After bug fixing, this is the new feature with minor alteration”

Commit this with message “ Added feature2.1 in development branch”

  • Line3>> This is the advancement of previous feature

Commit this with message “ Added feature2.2 in development branch”

  • Line4>> Feature 2 is completed and ready for release

Commit this with message “ Feature2 completed”

  • All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).

Task-03

  • In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:

  • Line to be added after Line3>> This is the advancement of previous feature

  • Line4>>Added few more changes to make it more optimized.

  • Commit: Optimized the feature

Here, I have checked out in dev branch and added a new commit “Optimized the feature”


I am Sunil kumar, Please do follow me here and support #devOps #trainwithshubham #github #devopscommunity #devops #cloud #devoparticles #trainwithshubham

sunil kumar

Shubham Londhe

Connect with me over linkedin : linkedin.com/in/sunilkumar2807