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 git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.
✔️Resolving Conflicts:
Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with the merge/rebase. git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files.
✔️Task-01
Create a new branch and make some changes to it, before that clone the github repo and get in to the folder.
mkdir devops cd devops mkdir git task cd git task #created two folders git check out -b new branch #i kept my new branch name as a "new branch" in my github repo git branch #now you will be on the "new branch" ls
Use git stash to save the changes without committing them.
git status git stash #saved working directory and index state WIP on "new branch": f66fc2c Added new file
Switch to a different branch, make some changes and commit them.
git check out dev #Another branch name "dev" vim version01.txt vim file1.txt git add . git commit -m "Added new file"
Use git stash pop to bring the changes back and apply them on top of the new commits.
git checkout new branch #switched to branch "new branch" git stash apply git add . git commit -m "added new version" git log --oneline
✔️Task-02
In version01.txt of development branch add 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 message “ Added feature2.1 in development branch”
Line3>> This is the advancement of previous feature
Commit this with message “ Added feature2.2 in development branch”
Line4>> Feature 2 is completed and ready for release
Commit this with
message “ Feature2 completed”
All these commits messages should be reflected in Production branch too which will come out from Master branch (Hint: try rebase).
git checkout dev #swiched to the "dev" branch git commit -m "added feat2.1 in development branch" vim version01.txt git commit -m "added feat2.2 in development branch" git add version01.txt git commit -m "added feat2.2 in development branch"
git checkout main
#now switched to main it will show how many commit you have done after switching to main branch
git rebase main
git log --oneline
git rebase dev
git log --oneline
#the files are getting added
✔️Task-03
In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:
Line to be added after Line3>> This is the advancement of previous feature
Line4>>Added few more changes to make it more optimized.
Commit: Optimized the feature
git cherry-pick <commit ID> #Auto-merging version01.txt file vim version01.txt git add version01.txt git commit -m "Optimized the feat" git push
Thank you for taking the time to read this blog....!😊