Day 11 : Advanced Git Topics (Part 2)

Day 11 : Advanced Git Topics (Part 2)

Git Advanced Topics (Part 2)

Introduction 💫

In this blog, we will learn more about Git. We will cover topics like git stash, git cherry-pick and resolve conflicts 🔺. These are some commands that are frequently used in the IT industry by developers. It is important to learn as you have to face these situations very often. 📝

Git Stash 🛍

Think of Git Stash as a secret compartment 🧺🔒 to temporarily hide your changes. It's like tucking your work away, so you can switch tasks without cluttering your current work. When you're in the middle of something and need to switch to another task or branch, use Git Stash. Stashing your work keeps it safe and allows you to return to it later.🔧📦

Assuming you're working on a coding project and you suddenly need to switch to another task without committing your current changes.🔁📊

  1. Check Status: First, use git status to see which files you've modified.

  2. Stash Your Changes: Use git stash to stash away your changes:

     git stash
    
  3. Switch Tasks: Now, you can switch to another branch or task without worrying about your ongoing changes.

  4. Check Stash List: To see your stashed changes, use:

     git stash list
    
  5. Reapply Stashed Changes: When you're ready to continue your previous work, use git stash pop:

     git stash pop
    
  6. Optional: Remove Stashed Changes: If you no longer need the stashed changes after applying, you can remove them with:

     git stash delete
    
  7. Clear All Stashes: To clear all stashed changes, use:

     git stash clear
    

Git Cherry-Picking 🍒🌿

Think of Git Cherry-Picking as handpicking the best fruits 🍒🌿 from another branch and placing them in your basket. It's like bringing specific commits from one branch into another.

Use Git Cherry-Picking when you want to add specific commits from one branch to another. It's handy for grabbing valuable changes without merging entire branches.

How Git Cherry-Picking Works? 🍒🖐️:

  1. Identify the commits you wish to pick from another branch.

  2. Use git cherry-pick <commit> to transplant those commits into your current branch.

Hands-On Cherry-Picking! 🚀🍒🔧:

Suppose you have two branches: feature and main.

  1. Checkout Target Branch: Switch to the branch where you want to apply the cherry-picked commits.

     git checkout main
    
  2. Cherry-Pick Commits: Use git cherry-pick <commit_id> to bring commits from the other branch:

     git cherry-pick ABC123
     git cherry-pick XYZ789
    
  3. Resolve Conflicts: If there are conflicts, resolve them just like in a merge.

  4. Continue Picking: Continue cherry-picking other desired commits.

  5. Push Changes: Once cherry-picking is done, push the changes to the remote repository.

Resolving Conflicts 🔺

Imagine two people editing the same sentence in a document at the same time. When changes in different branches clash⚔️, Git is unsure which version to choose.

For example⚒:

Let's say you're merging a branch named feature into main and conflicts arise.

  1. Identify and Checkout: Identify the conflicted file using git status and then use git checkout <filename> to open it.

  2. Resolve the Conflict: Review and edit the conflicting sections, retaining the desired changes.

  3. Stage and Commit: After resolving, use git add <filename> and git commit to finalize the resolution.

  4. Push Changes: Once resolved and committed, push the changes to the remote repository.

Tasks 📚:

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

Certainly, let's walk through those steps:

Assuming you're working on a Git repository and have two branches: feature-branch and main.

  1. Create a New Branch and Make Changes:

     git checkout -b feature-branch
    

    Make some changes to your files.

  2. Stash Your Changes:

     git stash
    
  3. Switch to a Different Branch and Make Changes:

     git checkout main
    

    Make some changes and commit them.

  4. Apply Stashed Changes:

     git stash pop
    

This will apply the stashed changes on top of your new commits. If there are conflicts, you'll need to resolve them.

Remember, git stash acts as a temporary storage for your changes. It's like placing your work-in-progress in a safe spot while you handle other tasks.

✳Task-02

  • In version01.txt of the development branch add the 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 alterations”

    Commit this with the message “ Added feature2.1 in development branch”

  • Line3>> This is the advancement of the previous feature

    Commit this with the message “ Added feature2.2 in development branch”

  • Line4>> Feature 2 is completed and ready for release

    Commit this with the message “ Feature2 completed”

  • All these commit messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).

When you made some commits on a feature branch (test branch) and some in the main branch. You can rebase any of these branches. Use the git log command to track the changes (commit history). Checkout to the desired branch you want to rebase then execute the rebase command.

git rebase <branch name>

✳Task-03

  • In the Production branch Cherry pick Commit “Added feature2.2 in development branch” and added the below lines in it:

  • The line to be added after Line3>> This is the advancement of the previous feature

  • Line 4>>Added a few more changes to make it more optimized.

  • Commit: Optimized the feature

Switch to the branch where you want to apply the cherry-picked commits.

Use git cherry-pick <commit_id> to bring commits from the other branch:

Resolve conflicts and commit with the respective commit message.


✉Endcard:

🎉 Thank you for joining me on this insightful journey into the world of DevOps!

❤ If you found this blog helpful and informative, don't forget to give it a like!

🔄 Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!

👉 Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!

Follow me on Linkedin --> abdallah-qamar 👔

Stay tuned for Day 12...👋