Advance Git & GitHub for DevOps Engineers: Part-2

Advance Git & GitHub for DevOps Engineers: Part-2

Git Stash:

Git stash is a command that allows you to save changes you've made to a file or project, without committing them. This is useful if you want to temporarily set aside changes and work on something else, or if you're not ready to commit the changes yet. When you use git stash, your changes are saved in a "stash" and you can retrieve them later using the git stash apply or git stash pop commands.

You can apply the saved changes later by using the git stash apply command. This will restore the saved changes to the working directory and the staging area. You can also use the git stash pop command to apply the saved changes and remove them from the stash at the same time.

You can also use the git stash list command to view a list of all the saved stashes, and the git stash drop command to delete a specific stash. You can use the git stash branch command to create a new branch from a saved stash

Git cherry-pick

git cherry-pick is a Git command that allows you to apply a specific commit from one branch onto another branch.

For Example, you have a branch called dev that contains multiple commits, but you only want to apply one of those commits to your master branch. You can use git cherry-pick <hash> to apply the specific commit onto your master branch. When you use cherry-pick it will create a new commit to your master branch.

Task-01

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

    By using the below command create the new branch and make changes to feature1.txt

    git checkout -b dev

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

    git stash save "added some text to feature1.txt"

    This will save our changes to the feature1.txt file in a stash with the message "added some text to feature1.txt".

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

    Here I switched to branch master and add and commit changes to project1.txt.

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

This will bring back the changes we saved earlier to the feature1.txt file and apply them on top of the changes we made in the dev branch. We can then continue working on the changes in the dev branch and commit them when we are ready.

we used the git stash save command to save our changes. Alternatively, we can use the git stash push command, which provides more options for specifying what changes to save. Similarly, we can use the git stash apply command instead of git stash pop if we want to keep the saved changes in the stash.

Task-02

  • In feature1.txt of development, the branch adds the below lines after “Default feature”.

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

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

  • Line3>> This is the advancement of the previous feature

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

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

    Commit this with the message “ Feature2 completed”

  • All these commits messages should be reflected in the Production branch too which will come out from the Master branch.

    Use the command: git rebase dev

Task-03

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

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

  • Line 4>>Added a few more changes to make it more optimized.

  • Commit: Optimized the feature

    Now we cherry-pick the "Optimized the feature" commit from dev to the master branch by using the below command,

  • git cherry-pick 483d596

Thank you for reading the blog.

Suggestions are always welcome. Thank you !!