Using git's rerere feature to escape recurring conflict hell · GitHub
/" data-turbo-transient="true" />
Skip to content
-->
Search Gists
Search Gists
Sign in
Sign up
You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
{{ message }}
Instantly share code, notes, and snippets.
skipcloud/rerere.md
Created<br>February 7, 2020 09:15
Show Gist options
Download ZIP
Star
(1)
You must be signed in to star a gist
Fork
(0)
You must be signed in to fork a gist
Embed
Select an option
Embed<br>Embed this gist in your website.
Share<br>Copy sharable link for this gist.
Clone via HTTPS<br>Clone using the web URL.
No results found
Learn more about clone URLs
Clone this repository at <script src="https://gist.github.com/skipcloud/f1033afb4fa5681d69fa63458cc95928.js"></script>
" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-595cf698-9d43-4906-95ef-e10d49fe9083" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-sized-down" />
Save skipcloud/f1033afb4fa5681d69fa63458cc95928 to your computer and use it in GitHub Desktop.
Embed
Select an option
Embed<br>Embed this gist in your website.
Share<br>Copy sharable link for this gist.
Clone via HTTPS<br>Clone using the web URL.
No results found
Learn more about clone URLs
Clone this repository at <script src="https://gist.github.com/skipcloud/f1033afb4fa5681d69fa63458cc95928.js"></script>
" readonly="readonly" data-autoselect="true" data-target="primer-text-field.inputElement " aria-describedby="validation-d1e93e1d-71df-43c1-aa5e-31236132fedd" class="form-control FormControl-monospace FormControl-input FormControl-small rounded-left-0 rounded-right-0 border-right-0" type="text" name="gist-share-url-original" />
Save skipcloud/f1033afb4fa5681d69fa63458cc95928 to your computer and use it in GitHub Desktop.
Download ZIP
Using git's rerere feature to escape recurring conflict hell
Raw
rerere.md
Have you ever tried to merge two branches only to end up in conflict hell? You fix a bunch of conflicts only to run git merge --continue and be presented with the same conflicts. Repeat this process and after a few iterations you give up because it just isn't worth the pain and effort.
Would you be surprised to know that there is a git feature specifically for this problem? It's called rerere and I'm going to enrich your life with it now. (I'm going to talk specifically about merging but I think it also helps rebasing)
rerere stands for Reuse Recorded Resolution. The TL;DR version is you ask git to remember how you've resolved hunks in the past, and if the same one comes up for a file in future just redo what you did last time.
To enable this feature just run this lovely command git config --global rerere.enabled true. You can also turn it on by creating this directory in your projects .git/rr-cache, although the global setting is much clearer.
I'll try to take you through an example of how this works, bear with me it might get long.
We have our (tiny) project with only one file in it, which looks like this
└── user.rb
0 directories, 1 file
I branch off master to create a branch called dev and I add a line to user.rb. Now I would like to stage this change so I pull down staging and try to merge my dev branch but uh oh, someone has merged a change to staging affecting the same line in user.rb that I am editing.
/tmp/example [staging] » git merge dev<br>Auto-merging user.rb<br>CONFLICT (content): Merge conflict in user.rb<br>Automatic merge failed; fix conflicts and then commit the result.
We've all seen this before, a run of the mill conflict message. However if you were to have rerere enabled you would get this output
/tmp/example [staging] » git merge dev<br>Auto-merging user.rb<br>CONFLICT (content): Merge conflict in user.rb<br>Recorded preimage for 'user.rb'<br>Automatic merge failed; fix conflicts and then commit the result.
You can now see a new line saying Recorded preimage for 'user.rb'. Running git rerere diff right now will give you the current state of the resolution file:
/tmp/example [c013552] » git rerere diff<br>--- a/user.rb<br>+++ b/user.rb<br>@@ -1,5 +1,5 @@<br>->>>>>><br>+=======<br>+hello<br>+>>>>>>> commit from dev
You go about the usual conflict workflow, choose which changes to keep, and commit the result. If you run git rerere diff again, you see the recorded resolution:
/tmp/example [c013552] » git rerere diff<br>--- a/user.rb<br>+++ b/user.rb<br>@@ -1,5 +1 @@<br>->>>>>>
Running git merge --continue will apply your commit and tell you about the new resolution for our file:
/tmp/example [c013552] » git merge --continue<br>Recorded resolution for 'user.rb'.```<br>Let's undo that merge with...