git - 整洁、干净的压缩 git PR 并将提交 merge 为一个提交

标签 git github commit pull-request

大多数时候在 repos 中,我们看到一个 PR,然后是那个 PR 的 merge 提交,它只是说“Merged pull request #XXX from ...”。

但最近,我看到了一个紧凑的版本,其中 pull 请求者和提交者的头像重叠,并且 only one clean commit出现在历史中:

enter image description here

如何做到这一点?

我尝试过但不起作用的方法:


更新

one of my PRs 时它看起来像的一个例子以这种方式 merge :

enter image description here

结果:

enter image description here

最佳答案

2016年4月更新

GitHub 引入了一个选项 squash commits when merging ,因此您可以直接从其 Web UI 执行此操作:

Squash and merge

旧方案

刚找到这个workflow from the Meteor team (巧合的是,谢谢 @Emily ):

When you look at a pull request in the GitHub web interface, there's a very attractive "merge" button. NEVER USE THE MERGE BUTTON. It is an attractive nuisance. It leads to git history that's way more complicated than necessary: if the PR was filed a month ago, the commit's parent will be a very old revision that leads to way more lines than necessary in a graphical view of the git history. Plus, if you're using the merge button, that means that you didn't ever check out the code and try it yourself! The following is a better way to land pull requests.

首先,在您的存储库中,找到 .git/config 文件的 [remote "origin"] 部分并添加以下行:

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

确保将其添加到现有的提取行之前。现在,每次执行 git fetch 时,都会更新存储库中的所有 merge 请求!这是一次性更改,您可以永远直接访问 PR。

然后您只需git checkout pr/XXX 并直接处理更改。精选后的 git push origin 将创建紧凑的 PR:

git checkout pr/32
# ... test changes ...
git checkout master
git cherry-pick pr/32
git push 

enter image description here

唯一的缺点是 GitHub 不会在关闭时自动删除 PR 分支,但只需单击一下即可,作为交换,您可以获得更好的历史记录。

If the PR is multiple commits, the best thing to do is to check it out, rebase it onto your development branch, make whatever other changes you need, and merge it back to the development branch with an explicit merge commit. This is similar to what the GitHub merge button does, except that the merge button doesn't do the VERY IMPORTANT rebase step and so it leaves ugly spaghetti in the project's git commit history. To do this, run:

git checkout pr/32; git rebase devel; git checkout devel; git merge --ff-only pr/32

Then test and push.

If you'd like to combine some of the commits into a single commit, you can use interactive rebase by running git rebase -i devel instead. Some tutorials: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog

Unfortunately GitHub is not smart enough to detect that you've merged a PR by hand, so you'll need to manually comment and close the issue with a link to the relevant commit. Alternatively, make sure that the merge commit's message contains Fixed #123.


更新:Kahmali Rose 进行了进一步的更新,使 GitHub 能够检测到 PR 已 merge ,就像单击了邪恶的 merge 按钮一样:make sure to rebase and merge instead of cherry-picking .

关于git - 整洁、干净的压缩 git PR 并将提交 merge 为一个提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27208042/

相关文章:

linux - 使用 GitHub 部署 key 的 Git 远程 pull - 权限被拒绝

ruby-on-rails - 从 GitHub 运行 Ruby on Rails 应用程序

java - 如何在 hibernate 中提交批量插入?

windows - 在没有 Atlassian 帐户的情况下安装 SourceTree?

git - 无密码克隆 Gitlab 仓库

git - 用图形查看本地 git 提交

git - `go get` 和 `git clone` 之间的区别?

Git2go : How to handle simple merge conflicts

git - 如何在不首先进行克隆的情况下查看远程 Git 存储库的最新提交?

Git 帮助 UserInterfaceState.xcuserstate : warning: Merge conflict: File still marked as conflicted