TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The second-order-diff Git trick

180 pointsby malloc47over 12 years ago

16 comments

stasmover 12 years ago
It looks like what you're really looking for is a local branch that you never push to the remote, and 'git diff master...' with 'git rebase'.<p>Create a local branch, apply the sledgehammer, and start reviewing changes. Use 'git diff master...' to review changes. (This is short for 'git diff master...HEAD' which in turn stands for 'compare current branch's HEAD with the commit on master off of which you've branched.) 'git add -p' and commit changes that you like.<p>Iterate until you're happy with the result. You will have ended up with a few commits on your local branch. Use 'git rebase -i master' to squash all commit in a single one. Finally, check out the master branch and merge your local branch in, preferably with --ff.
评论 #5265364 未加载
DoubleClusterover 12 years ago
Why not just commit after the first step? You can then use the normal diff, and afterwards you just remove your temporary commit to make the final one.
评论 #5243998 未加载
评论 #5243661 未加载
pseutover 12 years ago
I think 'git add -p .' would have worked better for the specific example he gave, though. (Step through and stage change by change)
评论 #5245508 未加载
评论 #5243392 未加载
nigglerover 12 years ago
git stash has other cool features and use cases (for example, when you want to pull from upstream and you have conflicting changes.) <a href="http://git-scm.com/book/ch6-3.html" rel="nofollow">http://git-scm.com/book/ch6-3.html</a> is a nice summary and the manpage also gives some sample workflows.
评论 #5243070 未加载
tmeasdayover 12 years ago
Of course the problem here is that the GP can't actually be sure that every link he wanted to fix in the final step was fixed; just that the ones that _were_ fixed were fixed right!
评论 #5243213 未加载
gbogover 12 years ago
I used a similar technique for a less frivolous task. I had script dumping data in text format, and wanted to refactor it. I committed the result and could refactor brutally step by step, running the script at each step and reverting whenever the dumped data showed some diffs.
ibottyover 12 years ago
when i have an idempotent sledgehammer (i.e. i can apply it twice and get the same result) i usually just<p><pre><code> git add -p </code></pre> everything that is ok. git diff will only show you the differences against the index, so this works as well.
habosaover 12 years ago
This is a great tip, thanks! I never thought of using git diff to test things out, and I definitely didn't know you could diff against a stash.
评论 #5242980 未加载
zacharypinterover 12 years ago
I find this alias really useful for accomplishing similar goals:<p>alias gqc="git commit -m 'quick commit'"<p>The command is usually preceded by "git add ." (or alias "ga."). Making a commit ends up being more reliable for me than stash. Also, the commit stays with the branch, making it easy to switch to master branch, make or check an important change, then go back to what I was working on in the develop branch. Additionally, it makes rebasing a work in progress easier. Just gqc &#38;&#38; git pull --rebase.<p>When it comes time to push, I can just check the log for all the "quick commit" commits. If there's just one, then I make an amend commit. If there's more than one, I rebase interactively.<p>I suppose I could add a hook to make sure I never accidentally push a quick commit, but it hasn't been an issue yet (over the past year or so I've only made the mistake once).
评论 #5247831 未加载
twicover 12 years ago
Am i missing something, or is this nothing to do with Git? You could do this with any source control tool, or indeed with simple copies of the files. I've used exactly this approach without Git for years.
评论 #5244282 未加载
emillonover 12 years ago
A bit offtopic, but I think that you could have let the default `pandocCompiler` in Hakyll handle your old posts written in Textile format.
评论 #5244046 未加载
minhajuddinover 12 years ago
Nice tip, My sledgehammer is a git-sub script (<a href="http://minhajuddin.com/2011/12/13/script-to-do-a-global-search-and-replace-in-a-git-repository" rel="nofollow">http://minhajuddin.com/2011/12/13/script-to-do-a-global-sear...</a>) :)
shurcooLover 12 years ago
This is very useful, as the article describes in detail.<p>I've found it useful to be able do diffs of diffs in my work, hence I'm planning to add the ability to do that to my toolset. Combined with live editing, I think it's going to be quite neat.
评论 #5275165 未加载
wubbfindelover 12 years ago
I tend to 'git commit' and then 'git commit --amend' until I'm happy, then push to others.<p>I find it easier anyway. But the stash approach is interesting, thanks for sharing!
评论 #5244399 未加载
kapuzineralexover 12 years ago
Nice one, indeed!
cecilplover 12 years ago
This is the equivalent in Perforce of shelving your change, then redoing your work and diffing against your shelf.