Skip to main content

Command Palette

Search for a command to run...

Day 11: Advance Git & GitHub (Part-2)

DevOps Learning

Updated
3 min readView as Markdown
Day 11: Advance Git & GitHub (Part-2)

Git Stash:

    • Purpose: Temporarily saves changes in your working directory without committing them.

      • Use Case: When switching to a different branch but not ready to commit current changes.

      • Commands:

        • git stash: Saves changes and removes them from the working directory.

        • git stash list: Shows a list of stashed changes.

        • git stash drop: Deletes a specific stash.

        • git stash clear: Deletes all stashes.

Cherry-pick:

    • Purpose: Selectively applies specific commits from one branch to another.

      • Use Case: When you want to bring specific changes from one branch into another.

      • Command:

        • git cherry-pick <commit_hash>: Applies the specified commit to the current branch.

Resolving Conflicts:

    • Cause: Occurs during merges or rebases when conflicting changes exist between branches.

      • Manual Resolution:

        • git status: Shows files with conflicts.

        • git diff: Displays differences between conflicting versions.

        • git add: Marks resolved files as ready for commit.

Let's practice these essential Git concepts.

Task 1: Stashing and Reverting

Objective: Stash changes, switch branches, and then apply the stashed changes back.

  1. Create a New Branch:

    • Create a branch named “feature-branch” from the existing “master” branch.

    • Use the following command:

        git checkout -b feature-branch
      
  2. Add a File and Stash Changes:

    • Inside the newly created branch, add a text file named feature.txt.

    • Write some content in the file (e.g., “This is a new feature!”).

    • Commit your changes with an appropriate message (e.g., “Added feature to feature-branch”).

    • Realize there’s a bug in your feature. The content should have been “This is a fantastic feature!” instead.

  3. Stash the Changes:

    • Use git stash to temporarily save your changes without committing them.

    • Switch to a different branch (e.g., “dev” or “bug-fix”) to work on something else.

  4. Apply the Stashed Changes:

    • Switch back to the “feature-branch.”

    • Use git stash apply or git stash pop to bring back your stashed changes.

    • Verify that the content in feature.txt is now “This is a fantastic feature!”

  5. Push Your Branch:

    • Push your local branch to the remote repository (e.g., GitHub).

Task 2: Cherry-pick and Conflict Resolution

Objective: Cherry-pick commits from one branch to another and resolve any conflicts.

  1. Create Two Feature Branches:

    • Create two branches: “feature1” and “feature2.”

    • In each branch, add a file (e.g., feature1.txt and feature2.txt) with some content.

    • Commit your changes in both branches.

  2. Cherry-pick Feature1 into Master:

    • Switch back to the “master” branch.

    • Cherry-pick the changes from “feature1” into “master”:

        git cherry-pick <commit_hash>
      
    • Resolve any conflicts if they occur.

  3. Rebase Feature2 onto Master:

    • Still in the “master” branch, rebase “feature2” onto it:

        git checkout feature2
        git rebase master
      
    • Again, resolve any conflicts.

  4. Observe the Differences:

    • Compare the commit history and logs after cherry-picking and rebasing.

    • Note how cherry-picking creates individual commits, while rebasing keeps a linear history.

Thank you for reading😉.