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

Moiz AsifMoiz Asif
3 min read

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.

Usage examples:

# Stash changes
git stash

# Switch to a different branch
git checkout other-branch

# Work on other tasks, make commits, etc.

# Return to the original branch and retrieve stashed changes
git checkout original-branch
git stash pop

Cherry-pick:

In Git, cherry-pick is a command that allows you to select and apply a specific commit from one branch and apply it to another branch. It's like picking a single "cherry" (commit) from a tree and adding it to a different tree.

Here's a concise example of cherry-picking a commit:

# Switch to the target branch
git checkout target-branch

# Cherry-pick the desired commit
git cherry-pick abcdef1234

# Resolve conflicts if needed

# Commit the cherry-picked changes
git commit

# Continue with your workflow

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

Thank you for taking the time to read this blog.

0
Subscribe to my newsletter

Read articles from Moiz Asif directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Moiz Asif
Moiz Asif