DEV Community

Cover image for Day 11 Task: Advance Git & GitHub for DevOps Engineers: Part-2
On-cloud7
On-cloud7

Posted on

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

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

Image description

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.

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.
Enter fullscreen mode Exit fullscreen mode

• Cherry-pick :

Cherry-picking in Git stands for applying some commit from one branch into another branch. In case you made a mistake and committed a change into the wrong branch, but do not want to merge the whole branch. You can revert the commit and apply it on another branch.

Image description

The main motive of a cherry-pick is to apply the changes introduced by some existing commit. A cherry-pick looks at a previous commit in the repository history and update the changes that were part of that last commit to the current working tree. The definition is straight forward, yet it is more complicated when someone tries to cherry-pick a commit, or even cherry-pick from another branch.

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

git checkout -b new-branch
    # Make your changes
    git add .
    git commit -m "Changes in new-branch"
Enter fullscreen mode Exit fullscreen mode

Use git stash to save the changes without committing them.

  git stash
Enter fullscreen mode Exit fullscreen mode

Task-03

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

    git checkout different-branch
    # Make your changes
    git add .
    git commit -m "Changes in different-branch"
Enter fullscreen mode Exit fullscreen mode

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

   git stash pop
Enter fullscreen mode Exit fullscreen mode

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

1.In version01.txt of development branch, add the specified lines:

git checkout development
   # Edit version01.txt as specified
   git add version01.txt
   git commit -m "Added feature2.1 in development branch"
Enter fullscreen mode Exit fullscreen mode

Add Line3 and commit:

  # Edit version01.txt as specified
   git add version01.txt
   git commit -m "Added feature2.2 in development branch"
Enter fullscreen mode Exit fullscreen mode

Add Line4 and commit:

 # Edit version01.txt as specified
   git add version01.txt
   git commit -m "Feature2 completed"
Enter fullscreen mode Exit fullscreen mode

Reflect these commits in the Production branch using rebase:

 git checkout production
  git rebase development
Enter fullscreen mode Exit fullscreen mode

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

After adding the lines and committing "Optimized the feature," let's continue:

 # Continue from the previous steps
Enter fullscreen mode Exit fullscreen mode

Now, you need to rebase the Production branch to reflect the changes made in the Development branch:

  git checkout production
    git rebase development
Enter fullscreen mode Exit fullscreen mode

If there are conflicts, resolve them and continue the rebase process.

Once the rebase is completed, you may need to force-push the changes to the remote repository (be cautious with force-push as it rewrites history):

git push origin production --force
Enter fullscreen mode Exit fullscreen mode

This ensures that the Production branch reflects the changes from the Development branch, including the cherry-picked commit and the additional optimizations. If you encounter conflicts during the rebase, Git will guide you through resolving them.

Remember to exercise caution when force-pushing, especially if the branch is shared with others. If others are working with the Production branch, it's better to coordinate and communicate the changes to avoid conflicts.

Top comments (2)

Collapse
 
vivekpandey074 profile image
vivek pandey

Commenting to keep your motivation high🔥

Collapse
 
oncloud7 profile image
On-cloud7

Thank you so much @vivekpandey074