Git & GitHub | Advanced | Part 4

Git & GitHub | Advanced | Part 4

·

3 min read

In this blog, I will discuss a few advanced git topics like git stash, cherry-pick, and resolving conflicts when the merge or rebase operation is performed on the branches.

Git Stash

git stash is a command in the Git version control system that allows you to temporarily save changes that are made to the working directory, without committing them to the repository.

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.

How does git stash work?

  • When you run git stash, Git takes the changes made to track files in the working directory and saves them on a stack of "stashed changes".

  • This action reverts the working directory to the state of the most recent commit, allowing it to switch between branches or perform other operations without having to commit the changes.

  • The stashed changes can be retrieved by running git stash apply or git stash pop.

  • git stash apply applies the most recent stash and leaves it on the stash stack,

  • git stash popapplies the most recent stash and removes it from the stack.

  • A specific stash can be specified to apply or pop, by referencing it with a stash ID.

Cherry-pick

git cherry-pick is a command in the Git version control system that allows the application of a specific commit from one branch onto another branch.

This is useful when a single commit needs to be picked up from another branch and apply it to the current branch without merging the entire branch.

How does git cherry-pick work?

  • When git cherry-pick is run, the commit hash of the commit needs to be specified that needs to be applied.

  • Git will then create a new commit on the current branch with the same changes as the original commit.

  • If there are conflicts with the changes in the destination branch, Git will pause the cherry-pick process and ask you to resolve the conflicts manually.

Resolving Conflicts

In Git, conflicts can arise when there are conflicting changes made to the same file or code blocks by multiple contributors. Resolving conflicts is an important aspect of collaboration in Git, and it involves carefully reviewing the conflicting changes and making decisions about how to reconcile them.

Tasks

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.

Task 2

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

Line 2>> After bug fixing, this is the new feature with minor alterations”. Commit this with the message “Added feature2.1 in development branch”

Line 3>> This is the advancement of the previous feature. Commit this with the message “Added feature2.2 in development branch”

Line 4>> 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 Main branch (Hint: try to rebase).

Task 3

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


To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.

#Day11 #90DaysofDevops

Did you find this article valuable?

Support Sneha K S by becoming a sponsor. Any amount is appreciated!