Day 11: Mastering Git & GitHub (Part-2) for DevOps

Day 11: Mastering Git & GitHub (Part-2) for DevOps

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.

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

To use git cherry-pick, you first create two new branches and make some commits to them. Then you use the 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 the 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.

Tasks for hands-on

Task 1

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

Solution (steps):

  • Create a branch using the git checkout -b branch-1 command and add a txt file or any changes you want.

  • Use the git stash command to save changes without committing them in branch-1.

  • Switch and create a new branch by using the git checkout -b branch-2 command. Create a txt file. Use the git add . command to stage the changes and git commit -m "commit in branch 2" to commit the changes in branch-2.

  • Then use the git stash pop command. The changes you stashed in branch-1 have been applied on top of the commits on branch-2.

Task 2

  • In version01.txt of the development branch add the 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 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 commit messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).

Solution (steps):

  • First, switch to the dev branch by using the git checkout dev command.

  • Edit the version01.txt file and add line 2 "After bug fixing, this is the new feature with minor alterations”. Add the file using git add version01.txt and commit the file using git commit -m " Added feature2.1" in the dev branch.

  • Repeat the above step and add the 3rd line "This is the advancement of the previous feature". Add the file using git add version01.txt and commit the file using git commit -m "Added feature2.2".

  • Repeat the step and add the 4th line "Feature 2 is completed and ready for release" in the txt file. Add the file using git add version01.txt and commit the file using git commit -m "Feature2 completed".

  • Switch to the master branch by using the git checkout master command.

  • In the master branch, use the git rebase dev command to merge the changes of the dev branch into the master branch. The commits made in the dev branch including their commit messages, have been rebased onto the master branch.

Task 3

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

  • 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

Solution (steps):

  • Create and switch to the production branch by using the git checkout -b prod command.

  • To cherry-pick the commit, use the git cherry-pick "<commit-hash>" command. Add the actual commit hash of feature2.2.

  • Edit the version01.txt file and add the line after the 3rd line "This is the advancement of the previous feature". Add another line "Added a few more changes to make it more optimized".

  • Now stage the file by using the git add version01.txt command and commit the file using the git commit -m "Optimized the feature" command.

  • Now, you have successfully cherry-picked the "feature 2.2" in the dev branch branch and committed it to the production branch.

<Thank you, that's all for today and follow to join me in the journey of DevOps>