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.
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.
Git Mastery: Branching, Stashing, and Popping Changes
Create a new branch and make some changes to it.
git checkout -b feature-2 git add feature-2 git commit -m "made changes in feature-2"
Use
git stash
to save the changes without committing them.git stash
Switch to a different branch, make some changes and commit them.
git checkout feature-1 git add . git commit -m "made changes in feature-1"
Use
git stash pop
to bring the changes back and apply them on top of the new commits.git stash pop
Integrating Bug Fixes and Advancement in Development and Production
In version01.txt of the development branch,
Add lines after the bug fix:
git add version01.txt git commit -m "Added feature2.1 in development branch"
Add more lines:
git add version01.txt git commit -m "Added feature2.2 in development branch"
Complete Feature 2:
git add version01.txt git commit -m "Feature2 completed"
Now, switch to the Production branch:
git checkout master git checkout -b production
Rebase to reflect the changes in the Production branch:
git rebase development
Cherry-Picking and Optimizing Feature2.2 with Advanced Changes
Cherry-pick the commit "Added feature2.2 in development branch" into the Production branch:
git checkout production
git cherry-pick <commit_hash>
Edit version01.txt to add more lines:
git add version01.txt
git commit -m "Optimized the feature"
Subscribe to my newsletter
Read articles from Raqeeb Ahmed Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by