Advance Git & GitHub for DevOps Engineers: Part-2

Advance Git & GitHub for DevOps Engineers: Part-2

day11 of #90daysofdevops

Git Stash

Git stash is a command that allows you to save your current working directory and index in a temporary location, so that you can return to it later. This is useful if you want to start working on something new, but don't want to lose the changes you've already made.

To use git stash, simply run the following command:

git stash

This will create a new stash entry, which will contain a copy of your working directory and index. You can then switch to a different branch, or start working on something new.

To restore your stash, run the following command:

git stash pop

This will restore your working directory and index to the state they were in when you created the stash.

You can also use the following command to list all of your stash entries:

git stash list

This will show you the name of each stash entry, as well as the date and time it was created.

Finally, you can use the following command to delete a specific stash entry:

git stash drop <stash_name>

Replace <stash_name> with the name of the stash entry you want to delete.

Here are some additional things to keep in mind about git stash:

  • Stash entries are stored in a hidden directory called .git/stash.

  • You can create multiple stash entries.

  • Stash entries are temporary, and will be deleted automatically if you don't use them.

  • You can use the git stash apply command to apply for a stash entry without actually popping it. This can be useful if you want to test out the changes in a stash entry without committing them.

Cherry-Pick

Cherry-pick is a Git command that allows you to apply the changes from a single commit to another branch. This can be useful for applying bug fixes or other changes to a branch that you don't want to merge with your current branch.

To cherry-pick a commit, use the following command:

git cherry-pick <commit-hash>

Replace <commit-hash> with the hash of the commit that you want to cherry-pick.

For example, to cherry-pick the commit with the hash 1234567890, you would use the following command:

git cherry-pick 1234567890

Cherry-picking a commit will create a new commit in the current branch with the same changes as the original commit. The new commit will have a different hash, so it will be considered a separate commit.

Here are some of the options that you can use with the git cherry-pick command:

  • --no-commit: This option will not create a new commit. Instead, the changes from the original commit will be applied to the current working tree.

  • --force: This option will force the cherry-pick to be applied, even if there are conflicts.

  • --allow-empty: This option will allow you to cherry-pick an empty commit.

Cherry-picking can be a useful tool for applying changes to a branch without merging it with your current branch. However, it is important to note that cherry-picking can create duplicate commits, so it should be used sparingly.

Resolving Conflicts

When you merge or rebase branches that have diverged, it is possible that there will be conflicts. This is because the two branches may have made changes to the same files. Git will not be able to merge these changes automatically, so you will need to manually resolve the conflicts before Git can proceed with the merge or rebase.

To resolve a conflict, you can use the following steps:

  1. Use the git status command to see a list of files that have conflicts.

  2. Use the git diff command to see the differences between the conflicting versions of the file.

  3. Open the file in a text editor and resolve the conflicts manually.

  4. Use the git add command to add the resolved file to the index.

  5. Use the git commit command to commit the resolved changes.

Once you have resolved all of the conflicts, Git will be able to merge or rebase the branches.

Tasks

Task 1

  • 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.

Task 2

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