#90DaysOfDevOps Challenge - Day 11 - Advanced Git & GitHub for DevOps Engineers Part 2
Welcome to Day 11 of the #90DaysOfDevOps challenge. In today's continuation of our exploration of Git, we will dive deeper into advanced techniques such as stashing, cherry-picking and conflict resolution. These techniques play a vital role in ensuring efficient collaboration and effective version control in software development.
Git Stash: Saving Changes for Later
Git stash
is a handy command that allows you to temporarily save changes you've made in your working directory without committing them. This comes in handy when you need to switch to a different branch to work on a separate task but want to keep your current changes.
To use Git stash
, create a new branch and make some changes. Then, by running git stash
, you can save those changes. This action removes the changes from your working directory and stores them in a new stash. You can view your stashed changes using git stash list
and delete specific stashes with git stash drop
. If you want to clear all stashes, use git stash clear
.
Cherry-Pick: Selective Commits
Git cherry-pick
is a powerful command that allows you to choose specific commits from one branch and apply them to another. This technique is useful when you only want to incorporate specific changes from one branch into another, without merging the entire branch.
To cherry-pick commits, create two branches and make commits to each of them. Then, using git cherry-pick <commit>
, you can select the specific commits you want to apply from one branch to the other. This provides fine-grained control over which changes are included in your target branch.
Resolving Conflicts: Bridging the Gap
Conflicts can arise when merging or rebasing branches that have diverged. Resolving conflicts is a crucial skill in Git, and Git provides helpful commands to streamline the process.
When conflicts occur, use git status
to identify the files with conflicts. By running git diff
, you can examine the differences between the conflicting versions, helping you understand the conflicting lines. To resolve conflicts, manually edit the conflicted files, keeping the desired changes and removing the conflict markers. After resolving conflicts, use git add
to stage the resolved files. This informs Git that the conflicts have been resolved and allows the merge or rebase process to proceed smoothly.
Task 1: Stashing and Applying Changes
In this task, we will work with branches and use the git stash
command to save changes without committing them. Follow the steps below:
Create a new branch using the command:
git checkout -b day11
Make some changes to the files in this branch.
Use
git stash
to save the changes without committing them:git stash
Switch to the
main
branch using the command:git checkout main
Make some changes to the files in the new branch and commit them.
Use
git stash pop
to bring back the changes you stashed and apply them on top of the new commits:git stash pop
Task 2: Rebase and Commit Messages
In this task, we will work with the version01.txt
file in the development branch and perform a rebase operation. Follow the steps below:
Open the
version01.txt
file in the development branch.Add the following lines after "This is the bug fix in the development branch" that you added on Day 10 and reverted to this commit:
After bug fixing, this is the new feature with minor alteration
Commit this change with the message "Added feature2.1 in the development branch."
Add the following line after the previous commit:
This is the advancement of the previous feature
Commit this change with the message "Added feature2.2 in the development branch."
Add the following line after the previous commit:
Feature 2 is completed and ready for release
Commit this change with the message "Feature2 completed."
Switch to the
main
branch and usegit rebase
to add all the commits from thedev
branch into themain
branch
Task 3: Cherry-picking and Optimizing Features
In this task, we will cherry-pick a specific commit from one branch to another and make further changes to it. Follow the steps below:
Switch to the
production
branch.Cherry-pick the commit "Added feature2.2 in development branch" using the command:
git cherry-pick <commit-hash>
Add the following lines after "This is the advancement of the previous feature":
Added few more changes to make it more optimized.
Commit this change with the message "Optimized the feature."
Congratulations on completing Day 11 of the #90DaysOfDevOps challenge! Today, we explored advanced Git techniques such as stashing, cherry-picking, and rebasing. These techniques are powerful tools in managing changes and collaborating effectively within Git repositories.
Stay tuned for Day 12 of the #90DaysOfDevOps challenge, where we will wrap up our Linux and Git session, diving deeper into their features and techniques that will serve as a strong foundation for our next adventure with Python from Day 13.