~jan0sch/darcs-book
Showing details for patch e0da8bb5120fc430476e920a602cfee15897f181.
diff -rN -u old-darcs-book/en/04-happy-little-accidents.md new-darcs-book/en/04-happy-little-accidents.md --- old-darcs-book/en/04-happy-little-accidents.md 1970-01-01 00:00:00.000000000 +0000 +++ new-darcs-book/en/04-happy-little-accidents.md 2024-11-24 07:52:21.668254105 +0000 @@ -0,0 +1,120 @@ +Happy little accidents +====================== + +Most of us are human, therefore we are bound to make mistakes. Version control +is a tool that keeps us safe, well at least our code. + +Imagine you have a piece of code that is working perfectly fine but now, for +some reason, you need to change it. If you are not using version control you +might have to backup all of your code before trying anything new. Things like +that can keep us from trying out better approaches because we might lose +precious work. With version control this is a none issue, you can always go +back to a previous state and start over. Or you can branch off and try something +completely different that might replace your existing project with a new and +improved version. It's a lot less stressful than the manual copy and paste +approach. + +In this chapter I want to show you ways to deal with mistakes. No more fear of +changing things, you can always back. Version control gives you a couple of +extra lives :) + +Reverting changes +================= + +The most obvious thing that might go wrong in your development process is that +you have a lot of changes in your working directory and you have decided that +they don't make the cut. You want to get rid of them. + +Here's an example. Imagine you are writing a novel and some time light at night +in some sleep depraved state you decide to rename the novel's protagonist from +"Sakura" to "Humungus". I surely sounded like a great idea at the time so you +fire up your text editor and make a search and replace over the whole project. +What you didn't remember is that the name "Humungus" is already mentioned +throughout the novel but it's late and you decide to go to sleep. The next +morning you wake up and realize what a mess you have made. You can't go search +and replace "Humungus" with "Sakura" anymore and your text editor decides that +undoing that change is not something it wants to do. Okay, I'm reaching there +but let's pretend, there are situations quite similar to this and some of you +might have come across them, I certainly did, and they are no fun. + +In a situation like this `darcs revert` is exactly what you want. It allows you +to restore the state of the working directory as if you didn't make any changes +since the last record. + +``` +$ cat story.txt +Sakura was looking at her creation. She was quite happy with it. +$ sed -i '' 's/Sakura/Humungus/' story.txt +$ darcs whatsnew +hunk ./story.txt 1 +-Sakura was looking at her creation. She was quite happy with it. ++Humungus was looking at her creation. She was quite happy with it. +$ darcs revert +hunk ./story.txt 1 +-Sakura was looking at her creation. She was quite happy with it. ++Humungus was looking at her creation. She was quite happy with it. +Shall I revert this change? (1/1) [ynW...], or ? for more options: y +Do you want to Revert these changes? [Yglqk...], or ? for more options: y +Finished reverting. +$ darcs whatsnew +No changes! +``` + +Done! `darcs replace` will prompt you for any change, asking whether it should +`revert` the change or not. Keep in mind that you can always hit `?` if you are +unsure about what the available options are. Here's a look at what you can do +when reverting. + +``` +How to use revert: +y: revert this change +n: don't revert it +w: wait and decide later, defaulting to no + +e: interactively edit this change + +s: don't revert the rest of the changes to this file +f: revert the rest of the changes to this file + +v: view this change in full +p: view this change in full with pager +l: list all selected changes +x: view a summary of this change + +d: revert selected changes, skipping all the remaining changes +a: revert all the remaining changes +q: cancel revert + +j: skip to next change +k: back up to previous change +g: start over from the first change + +?: show this help + +<Space>: accept the current default (which is capitalized) +``` + +If you just want to get rid of everything in your working directory without +being prompted interactively you can just issue `darcs revert --all` or the +short hand `darcs revert -a` and `darcs` will `revert` all the unrecorded +changes in your working directory. + +Unrevert +======== + +Even when correcting mistakes we are bound to make even more, thankfully `darcs` +still has got our back with `darcs unrevert`. Whenever we revert something +`darcs` keeps track of our last `revert`. So let's say that we have just +reverted our name change and decided we wanted to go back just to take another +look. + +``` +$ darcs unrevert +-Sakura was looking at her creation. She was quite happy with it. ++Humungus was looking at her creation. She was quite happy with it. +Shall I unrevert this change? (1/1) [ynW...], or ? for more options: y +Do you want to Unrevert these changes? [Yglqk...], or ? for more options: y +``` + +And just like that, we have undone our `revert`. Isn't it nice to have an extra +safe guard just in case something goes wrong?