Day 11: Advance Git & GitHub for DevOps Engineers: Part-2
Git Stash:
Git stash is a Git command that allows you to temporarily save changes that are not yet ready to be committed. This is useful when you need to switch to a different branch or work on a different task, but don't want to commit incomplete changes to the current branch.
When you run git stash
, Git takes all the changes that you have made to tracked files and saves them in a "stash" - a stack of temporary commits. The changes are removed from your working directory, leaving you with a clean slate to work on.
Cherry-pick:
git cherry-pick
is a Git command that allows you to apply a specific commit or a range of commits from one branch to another. It is useful when you want to selectively apply changes from one branch to another, without having to merge the entire branch.
To use git cherry-pick
, you first need to identify the commit or commits that you want to apply. You can do this by looking at the commit history of the source branch and identifying the commit hashes of the desired changes.
Once you have identified the commit or commits, you can switch to the target branch and run the git cherry-pick
command followed by the commit hash(es). Git will then apply the changes from the specified commit(s) to the current branch.
If there are any conflicts between the changes being cherry-picked and the current state of the target branch, Git will prompt you to resolve the conflicts manually.
Task-01
Create a new branch and make some changes to it.
After creating branch dev we added a file stashdev to staging area, but now we need to create a branch dev1 and we dont want the file stashdev in branch dev1
So we can use the git stash command to stash the changes
Now if we switch to branch dev1, we continue adding another file and commit those changes
If we now pope out the stash, still we can see that new commits are at the top after doing stash.
If we do git stash, we can get the stash entries back into the working directory or the staging area.
Task-02
In version01.txt of development, the branch adds the 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 the message “Added feature2.1 in development branch”
Line3>> This is the advancement of the previous feature
Commit this with the message “Added feature2.2 in development branch”
Line4>> 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 Master branch (Hint: try rebase).
Make sure you are on the development
branch:
In this branch, added line2, line3 and line4 along with it's commit messages respectively after each line changes according to description
And then seeing the logs through git log command
Now we need to create a branch Prod through master, so we switch to master branch and observe the logs
Here, we create a Prod branch and then do a rebase by git rebase dev
command so that all the extra commits of dev will get attached to the tail of Prod as we can see through the logs.
Task-03
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
Make sure you are on the production
branch:
Cherry-pick the commit from the development
branch:
git cherry-pick <commit hash of development branch>
Edit the file, add it and commit.
git add <file name>
git commit -m "Optimized the feature"
Push the changes to the production
branch:
git push origin prod
Thank you :)
Shubham Jangale