Day11/90DaysofDevOpschallenge-Advance Git & GitHub for DevOps Engineers: Part-2
Table of contents
Git stash:
In Git, the git stash
command is used to save changes that are not ready to be committed yet, allowing you to switch to a different branch or work on a different task without committing incomplete work. It temporarily stores your modifications and reverts the working directory to the last committed state.
Run
git stash
to save your changes. Git will create a new stash that includes all modified and staged files.git stash show
shows the changes recorded in the latest stashIf you have untracked files that you want to include in the stash, use the
--include-untracked
or-u
option:git stash --include-untracked
.You can also provide a message to describe the stash using the
--message
or-m
option:git stash save "My stash message"
.To retrieve your changes and apply the stash, use
git stash apply
.If you want to remove the applied stash from the stash list, use
git stash drop
orgit stash pop
.To see a list of stashes, use
git stash list
. It will display the stash index, description, and the branch where the stash was created.
Cherry-pick:
In Git, the git cherry-pick
command is used to apply specific commits from one branch onto another branch. It allows you to select individual commits and apply them to a different branch, incorporating the changes made in those commits.
Use
git log
to find the commit hash(es) of the commit(s) you want to apply to another branch.Use
git checkout
to switch to the branch where you want to apply the cherry-picked commit(s).Run
git cherry-pick <commit-hash>
to apply a single commit to the current branch.If you have multiple commits to cherry-pick, provide their commit hashes one after another
git cherry-pick abc123 def456
.Resolving Conflicts:
In Git, a merge conflict occurs when you or any of your team members make conflicting changes to the same file from two different branches even if you’re not working with team members. If you’ve made changes to the same file from different branches and the changes are conflicting, there will be a merge conflict.
then Git automatically handles merging for you. But if there are conflicting changes you make to the same file, you have to resolve them manually.
git status
command shows the files that have conflictsgit diff
command shows the difference between the conflicting versionsgit add
command is used to add the resolved files.
Task 1 :
Create a new branch and make some changes to it:
Start by creating a new branch named "feature3" using git checkout -b feature3
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.
Task2:
# Switch to branch "development"
git checkout development
# Create a file version01.txt and add below lines.
# "This is the bug fix in development branch
# Line2>> After bug fixing, this is the new feature with minor alteration"
# Commit the changes with message "Added feature2.1 in development branch
git add version01.txt
git commit -m "Added feature2.1 in development branch"
# In the version01.txt file, add: Line3>> This is the advancement of the previous feature
git add version01.txt
git commit -m "Added feature2.2 in development branch"
# In the version01.txt file, add: Line4>> Feature 2 is completed and ready for release
git add version01.txt
git commit -m "Feature 2 completed"
# Switch to the Production branch
git checkout production
# Use rebase to apply the commits from the development branch to the Production branch
git rebase development
Task3:
# Switch to the Production branch
git checkout production
# Cherry-pick the commit "Added feature2.2 in development branch"
git cherry-pick <commitID of the commit “Added feature2.2 in development branch”>
# In the version01.txt file, add: Added few more changes to make it more optimized
git add version01.txt
git commit -m "Optimized the feature"
Thank you,
Happy Learning.