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.
💎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. If you want to resume your work on it, run git stash pop
.
You can also use git stash drop
to delete a stash and git stash clear
to delete all the stashes.
git stash
temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.👾🎛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 git cherrypick <commitid_hash>
command to select the specific commits from one branch and apply them to the other.
🍒 Here is an example of how to cherry-pick a commit:
- Make sure you are on the branch where you want to add a new commit.
git switch main_branch
2. Use git log to find the commit hash of the commit:
git log
or
git log --oneline //to get 7 digits hash commit
Copy the hash commit that you want to cherry-pick 🤓
3. Execute the following command to cherry-pick that commit hash.
git cherry-pick <commit-hash>
or
git cherry-pick feature_branch~2
How to cherry-pick multiple commits ?
If you want to pick all the commits from commit A to C (where A is older than c) run:
git log --oneline //get 7 digits hash commit
git cherry-pick A^..C //pick all the commits from A to C (including A)
If you need to exclude the first commit (Commit A in our case)
//If you want to ignore A itself
git cherry-pick A..C //pick all the commits from B to C (exclude A)
4. Push the changes to main branch
git push origin main_branch
Why do we need to "Cherry-Pick"?🤔
💀 Case 1 (Fixed some bugs):
Suppose you are working on feature_branch. In this branch, you solved 1 bug but urgently you need to leave this bug on master branch.
💡 Solution:
In this case, you can copy the bug fix commit from feature_branch and apply/past it onto master branch by Cherry-Pick.
💀 Case 2 (Accidentally commit to the wrong branch):
Suppose you have accidentally committed something important to the wrong branch.
💡 Solution:
Use the cherry-pick command. You can copy the commit in the correct branch. And then use git reset
to undo this commit from the wrong branch. 💯
💀 Case 3 (Copy specific feature to another branch to publish):
Suppose you are working with a large team on a large-sized project. Some changes/features are proposed by anyone team member and you want to apply some of them to your main project, not all.
You don’t want to merge a whole branch into another branch. You only need to pick one or two specific commits. To pick some changes to your main project branch from other branches is called cherry-picking.
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 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.
Check git log
🔖 Task 2
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”
🙈(I got to learn a very basic but useful option today 🐥, lemme tell you what happened today and how that small command saved me). So, while writing this blog today, I left for some work and when I came back, by mistake I committed this line3 with the message "Feature2 completed" instead "Added feature2.2 in development branch”. At first, I thought I missed committing feature2.1 in the git, but when I started reading the task, I realized I mistakenly committed this with the wrong message. So, I searched how I can rename the commit message, and here you go, I did it with a small command option:
git commit --amend -m "new_message"
🙈Line4>> "Feature 2 is completed and ready for release"
Commit this with message “ Feature2 completed”
Git log:
All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).
I found this use case for cherry-pick, however
rebase
could also be used here👍
🔖 Task 3
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"
📆 Let's get started with the last task🦾
Anddd....it's done! 😎
You've got feature2.2 along with optimized feature in production.
I would like to update my Github, so I am going to push all my changes from my local repo to Github.
Happy Learning! :)
Subscribe to my newsletter
Read articles from Ashmi Sinha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by