Advanced Git & GitHub for DevOps Engineers

Advanced Git & GitHub for DevOps Engineers

What is Git Stash?

With Git, you can save your uncommitted changes in a temporary file without committing or discarding them by using the stash command. When you need to swap branches or work on something different without losing your modifications, this is helpful.

Here are some typical applications for git stash:

  1. Changing branches without losing unfinished work

  2. Focusing on anything else while keeping your present tasks undone

  3. Prior to making a dangerous change, save your changes.

To list all of your stashed changes, run the following command:

git stash list

To apply the most recent stash entry, run the following command:

git stash apply

To delete the most recent stash entry, run the following command:

git stash drop

\>>> What is cherry-pick?

Cherry-picking in Git refers to the act of choosing particular commits from one branch and merging them into another branch. Merging, on the other hand, applies every commit from one branch to another. There are several reasons why cherry-picking can be advantageous, including:

Applying particular commits to an alternative branch: The recommended practice is to cherry-pick commits that you wish to apply to a separate branch.

Reversing a commit: Cherry-pick can be used to undo changes that you have made if you wish to go back.

Porting commits to a different branch: If you have a branch that you want to keep separate from the main branch, but you want to apply some of the commits from the main branch, cherry-picking is a good option. To cherry-pick a commit, you will need to use the git cherry-pick command. The syntax for the command is as follows:

git cherry-pick <commit-hash>/commit-id

where <commit-hash> is the hash of the commit that you want to cherry-pick.

\>>>what is resolve conflicts?

In Git, resolving conflicts is the process of manually merging changes from two or more branches that have conflicting changes to the same files. Conflicts occur when two or more developers make changes to the same file, and those changes overlap. Git will mark the conflicting files and prevent you from merging until you have resolved them.

\>>> Task-01

Create a new branch and make some changes to it.Use git stash to save the changes without committing them.Switch to a different branch, make some changes and commit them.Use git stash pop to bring the changes back and apply them on top of the new commits.

The steps we are going to follow to complete above task :

Step 1: Create a new branch

git checkout -b new-branch

Step 2: Make some changes to the new branch

Make some changes to any files you want. For example, you could add a new file called hello.txt with the following content:

Hello, world!

Step 3: Save the changes without committing them

git stash

Step 4: Switch to a different branch

git checkout main

Step 5: Make some changes and commit them

Make some changes to any files you want on the main branch. For example, you could add a new file called world.txt with the following content:

Goodbye, world!

Commit the changes you made to the main branch:

git add world.txt

git commit -m "Added world.txt file"

Step 6: Bring the changes back and apply them on top of the new commits

Switch back to the new-branch branch:

git checkout new-branch

Apply the stashed changes:

git stash pop

The changes you made to the new-branch branch before stashing them should now be applied on top of the changes you made to the main branch. You should now have both the hello.txt and world.txt files in your working directory.

\>>> 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).

The steps to add the specified lines to version01.txt of the development branch, commit them with appropriate messages, and ensure those changes are reflected in the production branch:

Step 1: Switch to the development branch

git checkout development

Step 2: Edit version01.txt and add the lines

Open version01.txt in a text editor and add the following lines after the line "This is the bug fix in development branch":

Line2>> After bug fixing, this is the new feature with minor alteration”

Line3>> This is the advancement of previous feature

Line4>> Feature 2 is completed and ready for release

Step 3: Commit the changes

git add version01.txt

Commit the changes with the following messages:

git commit -m "Added feature2.1 in development branch"

git commit -m "Added feature2.2 in development branch"

git commit -m "Feature2 completed"

Step 4: Rebase development branch onto master

git rebase master

This will ensure that the changes made in the development branch are reflected in the master branch.

Step 5: Push the changes to the remote repository

git push origin development

This will push the changes made to the development branch to the remote repository.

Step 6: Create a production branch from master

git checkout -b production master

Step 7: Push the production branch to the remote repository

git push origin production

This will push the changes made to the production branch to the remote repository.

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

The steps to cherry-pick the commit "Added feature2.2 in development branch" into the production branch and add additional lines to it:

Step 1: Switch to the production branch

git checkout production

Step 2: Cherry-pick the commit "Added feature2.2 in development branch"

git cherry-pick <hash of the commit "Added feature2.2 in development branch">

Replace <hash of the commit "Added feature2.2 in development branch"> with the actual hash of the commit.

Step 3: Open the file version01.txt in a text editor

vim version01.txt

Step 4: Add the following lines after the line "This is the advancement of previous feature"

Line4>>Added few more changes to make it more optimized.

Step 5: Save the changes to the file and close the text editor

Step 6: Stage the changes

git add version01.txt

Step 7: Commit the changes with the message "Optimized the feature"

git commit -m "Optimized the feature"

Step 8: Push the changes to the remote repository

git push origin production

This will push the changes made to the production branch to the remote repository.


#Linux #DevOps #cloud #AWS #community #techenthusiast #CloudComputing #Devops


Follow for more:

https://www.linkedin.com/in/samarjeet-patil-921952251/