DAY 11 DevOps challenge
Advance Git & GitHub concepts for DevOps Engineers. part (2)
✔️ What is git stash ?
It allows you to temporarily store changes that are not ready for committing.
Stashed changes are saved separately from your commits.
You can use
git stash save
to create a new stash with an optional message.Stashes can be listed using
git stash list
and applied withgit stash apply
.Stashes are helpful when you need to switch branches or perform other operations without committing your changes.
The important thing is by using git stash
, you can keep your working directory clean and switch between branches or perform other tasks without losing your in-progress changes.
Some git stash commands
Command | Description |
git stash save "message" | Save changes to a new stash with an optional message. |
git stash list | List all stashes with their references. |
git stash show stash@{n} | Show changes stored in a specific stash. |
git stash apply stash@{n} | Apply changes from a specific stash to the working directory. |
git stash pop stash@{n} | Apply changes from a stash and remove it from the stash list. |
git stash branch branchname stash@{n} | Create a new branch from a stash and apply it. |
git stash drop stash@{n} | Delete a specific stash. |
git stash clear | Delete all stashes. |
✔️ What is cherry pick?
It allows you to select and apply individual commits from one branch to another.
Cherry-picking copies the changes introduced by the selected commit onto the current branch.
You can cherry-pick a single commit or multiple commits in a specified order.
It is useful for incorporating specific changes without merging or switching branches.
After cherry-picking, a new commit is created with the copied changes on the target branch.
By using git cherry-pick
, you can selectively apply commits from different branches, which can be helpful for incorporating bug fixes, feature enhancements, or other changes into your current branch without merging the entire branch.
Some git cherrypick commands in git
Command | Description |
git cherry-pick <commit> | Applies the changes of the specified commit onto the current branch. |
git cherry-pick <start>..<end> | Applies the changes of a range of commits onto the current branch. The range includes both the start and end commits. |
git cherry-pick -n <commit> | Applies the changes of the specified commit onto the current branch but does not create a new commit. Allows making further modifications before committing. |
git cherry-pick --continue | Completes the cherry-pick operation after resolving conflicts. |
git cherry-pick --abort | Aborts the cherry-pick operation and restores the branch to its original state before the cherry-pick started. |
git cherry-pick --quit | Aborts the cherry-pick operation but keeps any changes that have been applied. Useful when you want to save the changes but not continue with the cherry-pick. |
git cherry-pick --edit <commit> | Opens the commit message in an editor, allowing you to modify it before committing the cherry-picked changes. |
git cherry-pick --signoff <commit> | Appends a "Signed-off-by" line with your name and email to the commit message of the cherry-picked changes. |
git cherry-pick --strategy=recursive <commit> | Specifies a merge strategy to be used during the cherry-pick operation. The default is the 'recursive' strategy. |
git cherry-pick --no-commit <commit> | Applies the changes of the specified commit but does not automatically create a new commit. Allows making further modifications or combining changes. |
✔️ Resolving Conflicts
In Git, conflicts can arise when two or more developers make changes to the same file(s) and attempt to merge their changes together. Git is designed to detect and highlight such conflicts, and it is up to the developers to resolve them manually.
Resolving conflicts in Git involves identifying the conflicting changes and deciding how to integrate them into a new, merged version of the file. This can be done using Git's built-in merge tools or manually by editing the conflicting sections of the file.
✔️Task 1:
Create a new branch and make some changes to it.
git checkout -b stash-b git branch vim version01.txt cat version01.txt git add .
Use git stash to save the changes without committing them.
git add . git stash git status
Switch to a different branch, make some changes and commit them.
git checkout dev vim version01.txt cat version01.txt git add . git commit -m "version file commited"
Use git stash pop to bring the changes back and apply them on top of the new commits.
git stash list
git stash pop
git add .
git commit -m "git stash file commited"
git log --oneline
✔️Task 2:
In version01.txt of the development branch add the below lines after "This is the bug fix in development branch" that you added in Day10 and reverted to this commit.
git checkout -b prod git branch git checkout dev vim version01.txt :- "This is the bug fix in development branch" cat version01.txt git add version01.txt git commit -m "commited file on dev"
Line2>> After bug fixing, this is the new feature with minor alteration
Commit this with the message "Added feature2.1 in development branch"
vim version01.txt :- "After bug fixing, this is the new feature with minor alteration" cat version01.txt git add version01.txt git commit -m "Added feature 2.1 in development branch"
Line3>> This is the advancement of previous feature
Commit this with the message "Added feature2.2 in development branch"
vim version01.txt :- "This is the advancement of previous feature " cat version01.txt git add version01.txt git commit -m "Added feature 2.2 in development branch"
Line4>> Feature 2 is completed and ready for release
Commit this with the message "Feature2 completed"
vim version01.txt :- "Feature 2 is completed and ready for release" cat version01.txt git add version01.txt git commit -m "Feature2 completed"
All these commits messages should be reflected in the Production branch too which will come out from the Master branch.
git branch git checkout prod git rebase dev git log --oneline
✔️Task 3:
In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:
git log --oneline git cherry-pick <commit_id> vi version01.txt
Line to be added after Line3>> This is the advancement of previous feature
vi version01.txt :- "This is the advancement of previous feature" git add version01.txt git commit -m "adv feature"
Line4>>Added few more changes to make it more optimized. Commit: Optimized the feature
vi version01.txt :- "Added few more changes to make it more optimized" git add version01.txt git commit -m "Optimized the feature" git log --oneline
For another git related blogs.
Follow me on LinkedIn to see interesting posts like this : )