Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing temporary changes with Git

Tags:

git

debugging

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?

like image 806
kolypto Avatar asked Mar 27 '11 01:03

kolypto


2 Answers

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.

like image 58
Karl Bielefeldt Avatar answered Nov 01 '22 06:11

Karl Bielefeldt


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.

  1. Make sure you always inspect the diffs before committing. git diff /path/to/file will show you the changes and you can look for "Don't forget to...".
  2. You could automate this a little bit with grep: git diff | grep "Don't forget"
  3. You could even have a hook that checks for a regular expression (e.g. "Don't forget") and forbids commits that match. This could be in your local repository or in the repository you're pushing to.

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.

like image 33
Cameron Skinner Avatar answered Nov 01 '22 05:11

Cameron Skinner