The HIDDEN Git Trick for Resolving Merge Conflicts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
did you ever find yourself frustrated again in  front of a merge conflict ever thought to yourself   my God this is a trivial one I wish there was  something to solve this obvious step for me well   check this out there's a hidden feature in git  this is the docs words not mine that can magically   solve merge conflicts it's called get Riri and  it stands for reuse recorded resolution which in   simple words mean solve the current conflict like  I solved similar other ones before the feature is   off by default so in this video we're going to  switch it on see how to use it in real scenarios   where conflicts can be resolved automatically  for us if you're now thinking every conflict is   different why would I even use this well there  are two main reasons one that I actually try to   avoid is long living branches where Riri re helps  with solving continuous merges so that when you   eventually do merge to the main branch there's  nothing to solve the other which is more relevant   to my personal flow is keeping a branch rebased  with main where I keep handling the same conflicts   over and over because of ambiguity and while there  are other solutions to this like merge strategies   today we're focusing at this hidden functionality  that can help both developers and maintainers   constantly battling merging or rebasing with  conflicts with that in mind let's go in order to   enable rir re we have to configure it it usually  goes into the global git config and after setting   it up we can confirm it was properly set this  can also be fetched with Git config by fetching   re. enabled what we're going to go through is a  slightly improved version version of the process   you can take by reading the features docs on  the main git repository let's create a project   to test on so we make sure there's nothing either  interrupting the example or is preconditioned in   any way I'm using cargo to create a rust project  that by default has a Hello World app inside just   because it's easy and it's rust so why not if I  open main. RS the app prints hello world and can   be built and run successfully let's make sure the  git repo is initialized and holds a first commit   in the log since we're using the main branch once  ready I'll go ahead and Branch out into the very   original name another Branch where I'm going to  change hello to hola because rust isn't enough so   I want to practice my espanol as well I'll commit  the changes and yes the interface is neit link to   the video covering Neo is up here let's go back  to the original Branch from which we're branching   out yet again into another Super original name  yet another branch in alignment with the example   from the docks I'm going to change world to Moon  window and commit the change with neit okay here's   the deal now we have Master Branch holding hello  world we have another Branch holding hola world   and yet another Branch holding hello Mundo what  could possibly go wrong right let's go back to   the first Branch where I still hold my first  commit with Hello World I'll start by merging   another branch which goes okay main arrest now has  Hol World cool now let's merge yet another branch   and here we go aemerge conflict but you may have  noticed something different about this conflict it   says recorded pre-image for SRC main. RS we'll  touch later on what a pre-image is and what   it looks like but G is recording something about  this conflict yes of course this is thanks to Riri   being enabled now the same feature also stands on  its own now that we're in active merge conflicts   git reir status shows the file in question we can  also call the same with diff to visually print the   conflict itself and unsurprisingly hello Mundo and  hello world are are conflicting with each other if   we open main. RS the conflict is clear and while  we have a few methods to solve this today we're   going wrong to understand the basics I'll show  you a cooler way to solve merge conflicts at   the end so stick around but for now let's just  set the line to haa Mundo and resolve the issue   all that's left is to Stage the file and commit to  let git know we're ready now here's the cool part   git says recorded a resolution for SRC main. RS  meaning it now has a recollection of how a certain   conflict was resolved and if it sees something  similar in the future it'll hopefully know how to   handle it machine learning right let's put its  name to the test reset to head with carrot by   the way takes us one commit back so we're at hola  world again let's switch to the third branch with   hello Mundo and try to rebase the change we'll  rebase Master on the branch and while as expected   the process fails over a conflict catch this git  has resolved main. RS using previous resolution   yeah yep the recorded resolution earlier served  as memory and the same issue popped on a different   process but ririi already knew the scenario and  handles it automatically get magic if we run diff   it's beautifully shown how hola world and hello  Mundo automatically Rewritten as hundo by git   on its own why did the merge fail you ask well  git doesn't take full ownership over your code   and while it did solve the issue on paper you  have to be the one reviewing and committing it   as you're the author here's another way to use  V re and see it in action did you know you can   check out a conflict with check out conflict and  merg main. rs git recreates the merge conflict so   we can look at it just like any other commit in  the tree so now with the conflict back in place   we can resolve it with Git re re and see the same  resolution report kind of cool isn't it git status   still says we're in rebase as you have seen in  my prompt earlier this by the way is Starship   also written in Rust and there's a video All About  Starship right up here so with that let's move on   let's stage the file and continue the rebase  process small commit with the changes in place   and we're done just to see that no tricks  were made I'll go back to master and merge   the earlier problematic branch which now goes  through with no problems at all if we pop the   log it's straight as an arrow first commit hola  world and hundo as if nothing happened if you're   ever in doubt the help Pages for git RI are your  friend but honestly most of it is done for you   and as long as you remember the basics you're  good to go to see what rir is doing under the   hood let's take a look inside dogit where you'll  find a new rr- cach for recorded resolution cache   as a subdirectory holding different objects  since we only have one change recorded we   only have one hashed resolution and if we take  a pick inside it holds what we've seen earlier   a pre-image a post image and this image these  are just recorded conflicts and the resolution   G holds the pre-image as the predefined status  in this case Hello Mundo conflict then there's   this image for a current state to match and  finally the post image is what ririi would   use in order to solve the Matched conflict all  of a sudden it doesn't seem like pure magic but   just a hell of a useful feature within git no  one had ever heard of it's not farfetched to   assume we'll soon have an AI model serving the  same purpose don't worry I got your conflict   covered but for the time being you have to admit  this is pretty awesome especially for those who   rebase often like me annoyed by the repeating  conflicts I promised a cool merge conflict   resolution with Vim so check this one out I'll  quickly recreate another issue by adding a comment   on one branch it's going to say something and on  the other I'll say something else for the sake of   Simplicity once both committed I'll go back to  the original branch and create the same issue   as earlier with the conflict in place let me open  main. RS and zoom out a little bit so you can see   the next trick GV def split v stands for vertical  by the way living it out will split horizontally   this opens a three-way diff merge what we're  seeing here are three buffers the center piece   is my current conflict whereas the left and  right are two options we can pop telescope to   see the buffers and you can always find the left  option with a slash two in its name whereas the   other buffer has slash three this is good for  binding shortcuts picking one side or the other   if you want to bake this into your normal flow to  pick one we we can def get and tab lets me pick   a buffer let's go with three which has something  else and the pick is injected as a resolution all   that's left is a quick commit and we're done the  tree will show a branched out change that's merged   back let's not forget that this is a very simple  conflict and this method becomes way more powerful   when a conflict is more complex and requires  different Pickers to resolve everything as you've   seen sometimes your conflicts aren't trivial and  repeating them is simply not an option if you want   to dive deeper into three-way conflict resolution  in Vim go ahead and watch the full video covering   it and if you still don't have git integrated  into your development environment then you should   probably start with this video covering all the  plugins and configurations making Vim or neovim   the perfect frictionless environment thank you  for watching and I will see you on the next one
Info
Channel: DevOps Toolbox
Views: 18,126
Rating: undefined out of 5
Keywords:
Id: HJtxQPJUcJc
Channel Id: undefined
Length: 8min 48sec (528 seconds)
Published: Fri May 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.