Say, I'm on 'master' and have a blob:
DEBUG = FALSE
CACHE_SIZE = 100
code
code
code
Now I start debugging in a new branch...
DEBUG = TRUE # Don't forget to turn off!
CACHE_SIZE = 0 # Don't forget to set back to 100!
... fix some bugs, change some code... and merge my fixes back into 'master'. But unfortunately I have forgotten to return these "don't forget"-s to the original value.
How can I automate the process of returning some lines back to the original value? Or at least shorten it to a single command.
Maybe, some temporary commit, or stash, or some other technique?
Cameron has some good ideas for shorter term debug changes. I wanted to add a common one that works even for larger or more permanent sets of local debug changes, like if you commonly make the same "don't forget" changes every time you add a feature. I've heard it called a loom, quilt, stacked branches, and a pipeline. You can find plugins with those names to help maintain this kind of workflow, but there are subtle differences between them I've never really grasped, and the technique is not too difficult to do manually.
The basic idea is you add another branch between master and feature, let's call it debug. You make all your "don't forget" changes in that branch, then branch off again from debug to make feature, which contains all your changes that will go into production as normal. Then, to remove all your "don't forget" changes in feature, do:
git rebase --onto master debug feature
That makes it look like you branched straight from master and never added the changes in the debug branch. Then you merge into master like normal. The next time you want to add a feature, you just merge master into debug and your "don't forget" changes are automatically reapplied to the latest upstream code. Then just create a new feature branch from debug and the cycle starts again.
Obviously, you still have to remember to do the rebase before merging into master. Cameron's hook idea can be used to prevent merges if you forget.
There's probably no way to automate rollback of particular lines (since Git doesn't really have any knowledge of the semantics of file contents), but there are a number of ways you can stop this happening again in future.
git diff /path/to/file
will show you the changes and you can look for "Don't forget to...".git diff | grep "Don't forget"
Option 2 is probably the easiest. It still takes a bit of discipline - you need to make sure you always put "Don't forget" (or "TODO", or "FIXME" or whatever) in the comment and you need to run git diff | grep
, but that's not a lot of overhead.
Option 3 will make it easier in the long term to prevent this problem, especially if you are part of a team. Of course, anyone can change the comment to "Do not forget" (or just remove the comment altogether) and bypass the check, but it's better than nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With