当前位置: 动力学知识库 > 问答 > 编程问答 >

git: a clean patch for a branch

问题描述:

Say I have a master branch and a branch for developing a feature:

-- s -- x -- x -- x -- HEAD [master]

\

\

bs -- x -- x -- x [feature]

and the feature branch for some reason (laziness) is a bit old. Now when I'm on master and do git diff feature, I got all the diff from s to HEAD as well. I'd like to get a diff diff that actually happened on feature branch alone. But I don't know how to find out what s is. Is there a way to find out?

网友答案:

As Jonathon Reinhart noted you just need to diff commit s against the tip of feature. As you noted, you forgot to mention that you want to have git find commit s for you.

The general way to ask git to find s is to find the "merge base" between HEAD (master) and feature. The git merge-base command does this, so in a shell, you could do:

$ s=$(git merge-base HEAD feature)

and then

$ git diff $s feature

Conveniently, though, git diff takes over the three-dot syntax: git diff X...Y (note the three dots) means "find the merge-base of X and Y and then diff that merge-base against Y". So:

$ git diff master...feature

Or, since HEAD is currently a symbolic name for master, you can write:

$ git diff HEAD...feature

and this then means that you can simply write:

$ git diff ...feature

as omitting one of the names means "use HEAD". (Or, if you prefer and your git is not too ancient, you can use @ to spell HEAD.)

网友答案:

If you want to see the changes from s to feature, then:

git diff s feature

From the man page:

git diff [--options] <commit> <commit> [--] [<path>…​]
This is to view the changes between two arbitrary <commit>.

For all of your diffing needs, consult http://git-scm.com/docs/git-diff

分享给朋友:
您可能感兴趣的文章:
随机阅读: