让拉取请求只包含选定的文件
拉取请求(PR)提出将来源分支的更改合入目标分支。合并前没有单独选择文件的步骤。要让一个 PR 只包含特定文件,请从预期的目标分支创建新分支,只在这个分支上提交相关改动。
如果现有分支已经包含无关改动,再加一次提交并不会把它们从 PR 里移除。先准备一个只含目标改动的单独分支,再创建 PR。
参考:GitHub 拉取请求文档。
在 PR 评论里提及未关闭的议题
要在 PR 评论里引用议题,请输入 # 加议题编号(例如 #123)。输入 # 时,GitHub 会列出仓库里匹配的议题和 PR,供你选择。
GitHub 拉取请求显示的 diff 不对
我把 main 的改动压缩合并到 feature 后,又想把 feature 合回 main,就碰到了这个问题:压缩合并里的改动被列为差异,即使此时两个分支中的文件内容完全相同。
原因
https://stackoverflow.com/questions/55479729/github-pull-request-shows-wrong-diff GitHub 的界面虽然容易误导,但确实可以看到想看的结果。把其中一个点去掉即可。
把
https://github.com/account/repo/compare/branchA...branchB
改成
https://github.com/account/repo/compare/branchA..branchB
这样会直接比较两个分支的最新提交。原来的形式会先根据目标分支找出合并基点。
@edwin-evans 的回答引述了 GitHub 的回复,让我明白了其中原因:
This is a result of the type of diff we use on GitHub. We use git’s three dot diff, which is the difference between the latest commit on the HEAD branch and the last common ancestor commit with the base branch.
这段回复的意思是,GitHub 使用 Git 的三点 diff,比较来源分支的最新提交和它与目标分支最近的共同祖先。
我也问了 GitHub 支持团队,下面是他们的正式答复:
We show what's called a "three-dot diff" which is the difference between the latest commit on the HEAD branch and the last common ancestor commit with the base branch. So if you forked off of a repo a while ago and things have changed in the parent repo since then, or if you opened a PR from the master branch and then the master branch has changed in the meantime, the diff wouldn't show that.
It sounds like you'd rather see a "two-dot diff", which would show the changes between the two most recent commits on each branch. In future you can merge the base branch back into the head branch. That updates the last common ancestor and would be more likely to show you what you're expecting to see there.
他们解释,三点 diff 不会显示从共同祖先之后仅发生在上游仓库或目标主分支上的变化。若要比较两个分支各自的最新提交,可使用两点 diff。以后可以将目标分支合回来源分支,更新最近的共同祖先,看到的差异就更可能符合预期。
为什么这样做
原文给出的理由是,Git 不建议先在本地合并并解决冲突,然后向远程分支提出拉取请求。通过远程拉取和解决冲突,更便于维持清晰的历史和工作流程。
提交签名与 Verified 状态
提交签名、SSH 身份验证以及贡献归属各自回答不同的问题。关于 GPG 和 SSH 签名、移除密钥后仍然有效的验证,以及与电子邮件相关的贡献归属问题,详见在 GitHub 上为 Git 提交签名:GPG、SSH 与 Verified 状态。
引用此仓库
在仓库根目录创建 CITATION.cff 文件,GitHub 就能在侧栏显示“Cite this repository”按钮。
基本的 CITATION.cff 文件
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
title: "Your Project Name"
authors:
- family-names: "Your"
given-names: "Name"
email: "[email protected]"
orcid: "https://orcid.org/0000-0000-0000-0000" # optional
version: "1.0.0"
date-released: "2025-01-05"
url: "https://github.com/your-username/your-repo"
repository-code: "https://github.com/your-username/your-repo"
license: MIT
keywords:
- keyword1
- keyword2
abstract: "Brief description of your project."
引用关联的学术论文
如果项目有关联的论文,请添加 preferred-citation 段:
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
title: "Your Project Name"
authors:
- family-names: "Your"
given-names: "Name"
preferred-citation:
type: article
authors:
- family-names: "Your"
given-names: "Name"
title: "Paper Title"
journal: "Journal Name"
year: 2025
doi: "10.xxxx/xxxxx"
设置步骤
- 在仓库根目录创建
CITATION.cff - 提交并推送:
git add CITATION.cff git commit -m "Add citation file" git push - 验证:打开 GitHub 仓库,在右侧“About”下方查找“Cite this repository”
GitHub 会根据这个文件自动生成 BibTeX 和 APA 格式的引用。