#Devops Day11
Advance Git & GitHub for DevOps Engineers
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.
Ans:- Create a new branch

Switch to the new branch

Make changes to your code or files.
Stage and commit your changes:


Use git stash to save the changes without committing them.
Ans:- Stash your changes:

Switch to a different branch:

If you want to apply the stashed changes:

If you want to completely remove the stash:

Switch to a different branch, make some changes and commit them.
Ans:- Switch to the branch you want to work on:

Make changes to your code or files.
Stage the changes:

Commit the changes:


Use git stash pop to bring the changes back and apply them on top of the new commits.
Ans:-Stash your changes:

Make some new commits or changes:

Bring back the stashed changes and apply them on top:

Task-02
In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:
Ans:- To add lines to a commit after cherry-picking it to the production branch, follow these steps:
Cherry-pick the commit: First, ensure you're in the production branch:

Then, cherry-pick the commit you want:

Replace
<commit_hash>with the hash of the commit "Added feature2.2 in development branch".Amend the commit: After cherry-picking, amend the commit to add the additional lines.

This command opens your default text editor. Add the required lines after the existing content, save, and close the editor.
Push the changes: Once the commit is amended, push the changes to the production branch:
Line to be added after Line3>> This is the advancement of previous feature
Ans:- Assuming you have a file called
yourfile.txt:sed -i '3a\This is the advancement of the previous feature, simple and easy' yourfile.txt
This
sedcommand appends a line after the 3rd line of the file.If you're on a system without the
-i(in-place) option forsed, you can redirect the output to a new file and then replace the original file if needed:sed '3a\This is the advancement of the previous feature, simple and easy' yourfile.txt > temp_file.txt mv temp_file.txt yourfile.txt
Line4>>Added few more changes to make it more optimized.
Ans:- If you want to add another line after Line 4 in the same file, you can continue using
sed:sed -i '4a\Added few more changes to make it more optimized, easy and simple' yourfile.txt
Commit: Optimized the feature
Ans:-To commit the changes you made with
git stash popand the additional lines you added, you can follow these steps:Add the changes to the staging area:

Commit the changes:

This will commit the changes to the repository with the specified commit message. Make sure to replace
yourfile.txtwith the actual file name where you made the modifications.
