嗯,@{-1}
可能 是 A。但也有可能不是。它只是表示“我之前所在的分支”。同样地,查看 git reflog
可能会告诉您创建当前分支时所在的位置。
但当前分支本身并没有告诉您任何信息。真正的问题在于您对“分支”的理解与 Git 不同。“自从创建分支以来”这个短语可能会误导您。
您似乎认为 B 是“从 X 开始一直到 B 结束的所有内容”。其实不是这样的。B 只是一个提交:即您图表中标记为 B 的提交。倒退回到根提交的所有内容——B 之前的提交、再往前一次提交、X、X 之前的提交以及再往前一次提交——它们都有完全相同的状态。它们都是可以从 B 访问到的提交,那就是它们的全部内容。
所以,在 Git 的思维中,X 没有什么特别之处。您认为它很特别,因为它是 A 和 B“相遇”的地方。但要区分这一点,您必须知道 B 和 A 的名称。如果你想让 Git 帮助你,你必须将你看到的拓扑结构传达给它。
如果您愿意讨论A和B,那么可以使用git diff ...A
和git diff A...
来查找自X以来发生了什么变化。或者您也可以谈论git merge-base A B
来找到X。但是只有人类能够区分此处的关键是A。
A
的名称,但要知道一种算法来正确反映您所指的A
的语义。据我所知,@VonC的答案做到了这一点。 - Inigo但是:我非常懒。我不想记得我当前的分支是从哪个分支创建的。
那你需要编写一个脚本,并命名为git-diffca
,然后可通过git diffca
进行调用(ca代表“共同祖先”)
在该脚本中,您需要决定在所有候选分支中选择哪些标准:
c--c--c (C)
/
a--Y--X--a--a (A)
\
b--b--b (B, HEAD)
C
和B
分支之间共同祖先Y
的更改?还是来自A
和B
之间的共同祖先X
?git merge-base <aBranch> B
,并选择最近的提交(在此模式下为X
)git diff A...
(因为git merge-base A B
得出了与B的最近共同祖先)。git-diffca
? - guettliA
?t
选项:git checkout -tb B
你希望切换到一个新的分支 B
并跟踪你现有的分支,如分支 A
,对吧?注意,我不需要记住或输入它。 可以使用 git branch -u
选项在事后设置新的上游。
当你告诉 git 你正在跟踪哪个分支时,git diff @{u}...
或者 git diff @{upstream}...
就能做到你想要的事情。 你甚至可以将其作为别名:git config --global alias.du diff @{u}...
。
请注意,Git 通常会自动从远程设置上游分支的跟踪:如果你输入 git checkout fix32943
,并且你没有一个叫做 fix32943
的分支,但是只有一个远程存在 fix32943
分支,Git 将创建一个本地的 fix32943
分支并设置它跟踪该远程分支,就像你手动设置一样。因此,git diff @{U}...
通常已经能够按照你所期望的方式工作了。
git co -b foo
,编写代码,提交,推送。然后我看到一条消息:fatal: The current branch foo has no upstream branch.... use: git push --set-upstream origin foo
。我复制并粘贴了这行代码,然后创建了一个拉取请求。如果我使用 git co -tb foo
,那么这个方法就不再起作用了。悲伤,但却是真实的。 - guettlipush.default
配置,你可能会喜欢 current
或 matching
的功能。 - jthill我将重复@matt在他的答案中详细阐述的内容: git
跟踪 提交,但不跟踪 分支
然而,有一些东西看起来像是“该分支的生命周期”,它就是该分支的 reflog:
git reflog my/branch
git reflog my/branch | tail -1
# and use something like `| awk '{ print $1 }'` for the sha,
# or parse the 'branch: Created from ...' message (if there is one)
# to see if there is a branch name
git reflog --format="%H" my/branch | tail -1 # if you want only the sha
但是需要注意的是:如果在该分支的生命周期内运行了git rebase
,那么初始提交点可能不再是您正在寻找的分叉点。
另一个代理方法可能是:查看存在于分支B
历史记录中,且不存在于其他任何分支历史记录中的提交:
# the following will list all branches except 'my/branch' :
git branch --format="%(refname:short)" | grep -v "my/branch"
# here is a way to list all commits on 'my/branch', excluding commits
# from all other branches.
# Adding `--boundary` will also display the first 'hidden' commit
git log --boundary --oneline --graph my/branch --not $(<the command above>)
# you can also replace 'git log' with 'git rev-list', if you only want the hashes
如果你很幸运,被称为边界点的提交就是你要找的那一个。
需要注意的是:如果在你的分支历史中有一些合并,那么你可能会有几个“边界”点;如果还有另一个分支比你正在寻找的X
更近地从分支B派生出来,那么边界将是该分支与master
或develop
的分岔点,而不是与它。
根据您的评论进行更新:
# get the name of your current active branch :
mybranch=$(git rev-parse --abbrev-ref HEAD)
# make a separate function to list on stdout "all the branches except mine"
list_branches () {
# list the `master` branch
echo "master"
# get all 'origin/somefeature' branches, excluding mybranch
# suggestion: list remote branches (you will get all features,
# even if you haven't created a local checkout of a branch)
git branch -r --format="%(refname:short)" |\
grep "/feat/" |\ # you can grep for a pattern if you have one
grep -b "$mybranch" # remove your branch from the lot
}
# get the 'boundary' commits of your branch with respect to
# all the other branches
base=$(
git rev-list --boundary --graph "$mybranch" --not $(list_branches "$mybranch") |\
grep -e "^-" |\ # rev-list will prefix boundary commits with a '-'
tail -1 |\ # if there are several, only take the last one
tr -d '-' # delete that leading '-'
)
# with some luck, we computed the correct base commit :
git diff $base...
正如之前的每个人所说,git
跟踪提交而不是分支。您可以在分支之前标记提交以使其更容易,但这仍然需要您手动进行标记。
步骤:
git tag <some descriptive tag>
git push origin <tag name>
如果是单个标签
git push --tags
如果是多个标签然后,从这里您可以轻松查看日志并根据需要查看更改。
或者,您可以只命名您的分支为 branchName_branchedFrom
类型的命名约定,然后它将更容易跟踪(其中branchedFrom可以是short-sha1或如果您已经标记了它,则可以是标记名称。
事后修改不可能(执行 git diff A...
是最好最短的选择)。但是,如果你事先知道一个分支在另一个分支的上游,那么你可以在创建分支时使用 -t
/--track
选项设置“上游配置”:
git checkout --track --branch newbranch upstreambranch
要为现有分支配置上游,请使用带有-u
/--set-upstream-to=
选项的git branch
命令:
git branch --set-upstream-to=upstreambranch existingbranch
(在您的示例中:newbranch/existingbranch == C,upstreambranch = A)
然后,您可以使用{{link1:newbranch@{upstream}
}}或newbranch@{u}
(简称)访问上游分支的当前分支尖端提交。要获取当前分支(HEAD
)的上游分支,只需使用@{u}
即可。
您的命令将变为:
git diff newbranch@{u}...newbranch
或者,使用 newbranch
分支检出:
git diff @{u}...
git diff X...
?你想要比较A还是X? - Igal S.A...
是指的。请注意三个点。在git diff
中,它们表示“共同祖先”,即X
。git diff A..
将计算与A
的差异。请注意两个点。 - j6tmaster
、develop
和release-{xx}
),它们可以作为分支的有效“分叉点”? - LeGEC