Advance Git & GitHub for DevOps Engineers: Part-2

#day11 of #90daysofDEVOPS

Git Stash:

Ever found yourself in the middle of a coding spree, and suddenly realize you need to switch gears to tackle another task or dive into a different branch? But hey, you’re not quite ready to commit those changes yet. Enter Git stash — your trusty sidekick in these moments of coding chaos.

How it rolls: So, here’s the lowdown: Git stash swoops in and scoops up all your unsaved changes — the ones you’ve staged and the ones you haven’t — and tucks them away for safekeeping. It then magically rolls back your working directory to how it looked before you started tinkering away.

Now, here’s the cool part: when you’re ready to pick up where you left off, you can simply apply those stashed changes back onto your working directory. Think of it like unpacking your suitcase after a trip — only you’re unpacking your code changes. You’ve got two options here: you can either gently apply the latest stash without clearing it from your stash list (that’s git stash apply), or you can pop that stash right off the list and apply it in one swift move (git stash pop).

When to whip out the stash: Oh, the places you’ll go with Git stash! It’s your go-to move when you need to hop between branches without committing your changes, when you want to stash away tweaks temporarily while you troubleshoot, or even when you’re caught in the crossfire of merge conflicts.

Git Cherry-pick:

Picture this: you’re cruising along on one branch, and you spot a juicy commit — maybe it’s a bug fix or a slick new feature — on another branch. But you don’t want to merge the entire branch just for that one gem. That’s where Git cherry-pick struts onto the scene, ready to pluck that commit from its branch and drop it right into yours.

How it’s done: So, here’s the drill: you point Git to the commit you’ve got your eye on by waving its unique commit hash in its direction. Git then works its magic, cherry-picking those changes and plopping them down onto your current branch. Voilà! You’ve got yourself a shiny new commit sporting the same changes.

When to cherry-pick: Cherry-pick is your secret weapon for cherry-picking (pun intended) specific fixes or features from one branch to another. It’s like hand-selecting the ripest fruit from the Git tree without having to haul the entire harvest over.

Resolving Conflicts:

Ah, conflict — the inevitable bump in the road when merging or applying changes in Git. It’s like a puzzle waiting to be solved, and Git gives you all the tools you need to crack it.

How it plays out: When Git hits a snag and can’t smooth out the differences between changes from different sources, it flags those pesky conflict zones in your files. That’s your cue to step in and play referee, manually sorting through the changes to decide which ones make the cut.

Once you’ve untangled the mess and reached a truce, you let Git know by staging those files (that’s git add) and sealing the deal with a commit. It's like bringing peace to the code kingdom, one conflict at a time.

Strategies for peacekeeping: You’ve got options for how to tackle conflicts: you can roll up your sleeves and dive into manual resolution, using your trusty text editor to pick and choose the winning changes. Or you can call in reinforcements with Git’s merge tools for some visual aid.

Sometimes, you might decide to side with one set of changes over the other, or perhaps you’ll get crafty and merge bits and pieces from both sides to create a hybrid solution. The choice is yours — Git’s just here to lend a hand in keeping the peace.