Advance Git & GitHub for DevOps Engineers: Part-2

Suraj barikSuraj barik
4 min read

🚨Git Stash:

git stash is a Git command to locally store your recent changes in a separate area so you can fetch those changes later. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

if a developer is working on a project and wants to preserve the changes without committing them. This will allow him to switch branches and work on other projects without affecting the existing modifications. You can roll back modifications whenever necessary, and it stores the current state and rolls back developers to a prior state.

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. For example:

Note that git stash pop removes the stash after applying it. git stash apply leaves it in the stash list for later use. The below screenshot will remove the stash after applying it.

🍒Cherry-pick:

Like git merge, the git cherry-pick command is one you use to incorporate changes from other branches into your current branch. Cherry picking is the act of picking a commit from a branch and applying it to another.

how do i cherry pick a commit in Git

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.

git cherry-pick fde1550

Resolving Conflicts:

Merging means combining changes from one branch into another branch.

Resolving merge conflicts in Git is a common situation that occurs when Git cannot automatically merge changes from different branches. When this happens, Git will mark the conflicting files with conflict markers, and it's up to you to manually resolve the conflicts. Here's how you can do it:

  1. Identify the Conflict: When you attempt to merge branches, Git will inform you about the conflict and the affected files. You can also use git status to see which files have conflicts.

  2. Open the Conflicting File(s): Open the files with conflicts in a text editor. Inside the file, you will see sections marked with conflict markers, like this:
    <<<<<<< HEAD // Your changes in the current branch (e.g., master)
    ================
    // Changes from the other branch (e.g., dev)
    >>>>>>>>>>dev

  3. Resolve the Conflicts: Review the conflicting sections and decide which changes to keep. You can modify the content to include the desired changes or remove lines that are no longer necessary. Remove the conflict markers (<<<<<<<, =======, and >>>>>>>) once you've resolved the conflicts.

  4. Save the File(s): After resolving the conflicts, save the changes in the file.

  5. Stage the Changes: Use git add<file> to stage the resolved file once you have edited it.

  6. Complete the Merge: Once all conflicts are resolved and staged, use git commit to complete the merge commit.

    git commit -m "Merge branch 'dev' into 'master' "

  7. Review Your Changes: Use git log or any other Git visualization tools to review your merge commit and verify that everything looks correct.

  8. Push Your Changes: If you resolved the conflicts in a local repository and are pushing the merged branch to a remote repository, use git push to update the remote branch.

🧠Assignment

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

Here , we do three things first create file finder.txt on dev branch and put it onto staging area then without commiting put it into stash.

🚏Task-02 :

  • In version01.txt of development branch add below lines after “This is the bug fix in development branch” that you added in previous day 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

🚏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

Thanks for reading my article. Have a nice day.

0
Subscribe to my newsletter

Read articles from Suraj barik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suraj barik
Suraj barik

I'm Suraj Barik Aspiring DevOps Engineer with Hands-on experience in Automating,Shell Scripting, Supporting in AWS, management, CI/CD, and DevOps processes.