在 GitHub 上发送仅最新提交的拉取请求

发布于 2024-10-21 01:51:46 字数 262 浏览 1 评论 0原文

我在 github 上分叉了一个项目,并成功对我的本地 master 进行了更改并推送到 github 上的原点。我想发送拉取请求,但只想包含最后一次提交。 github.com 上的拉取请求 UI 显示了最后 9 个提交,但我不知道如何过滤掉它。

我试图了解是否应该创建一个新的本地分支,检查一下并以某种方式重置或变基到上游?然后将我的 master 按 id 的最后一次提交应用到新的本地分支并将其用于拉取请求?

我正在尝试正确理解概念并找出正确的命令行来完成我需要的操作。

I forked a project on github and am successfully making changes to my local master and pushing to origin on github. I want to send a pull request, but only want to include the last commit. The pull request UI on github.com shows the last 9 commits and I don't know how to filter that down.

I was trying to understand if I should create a new local branch, check that out and somehow reset or rebase to upstream? Then apply my last commit from my master by id to the new local branch and use that for the pull request?

I'm trying to get the concepts right and figure out the right command lines to do what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半衾梦 2024-10-28 01:51:46

您基本上需要创建一个新的分支并创建一个新的分支。 樱桃-选择您想要添加到其中的提交。

注意:在 checkout/cherry-pick 命令之前您可能需要这些

git远程添加上游

git远程更新

git checkout -b <new-branch-name> upstream/master

git cherry-pick <SHA hash of commit>

git push origin <new-branch-name>

之后,您将看到 github 上的分支,切换到它并可以提交包含您想要的更改的拉取请求。

You need to basically create a new branch & cherry-pick the commits you want to add to it.

Note: you might need these before the checkout/cherry-pick commands

git remote add upstream <git repository>

git remote update

git checkout -b <new-branch-name> upstream/master

git cherry-pick <SHA hash of commit>

git push origin <new-branch-name>

Afterwards, you will see <new-branch-name> branch on github, switch to it and can submit the pull request with the changes you want.

俯瞰星空 2024-10-28 01:51:46

从最新提交开始创建一个新分支,该提交也在原始存储库中:

git branch new-branch origin/master
git checkout new-branch

然后使用 gitcherry-pick 获取您想要拉取请求的单个提交。如果具有此提交的分支称为 feature 并且您想要的提交是此分支中的最新提交,这将是

git cherry-pick feature

假设此补丁应用没有冲突,您现在有了一个可以执行您的操作的分支拉取请求。

第二步,您现在需要决定如何处理您的 feature 分支。如果您尚未在此分支上发布更改,则最好的过程可能是在新分支上重新调整此分支(并删除最后一次提交,如果 git rebase 没有自动完成此操作)。

Create a new branch starting from the latest commit, which is also in the origin repository:

git branch new-branch origin/master
git checkout new-branch

Then use git cherry-pick to get the single commit you want the pull request for. If the branch with this commit is called feature and the commit you want is the latest commit in this branch, this will be

git cherry-pick feature

Assuming this patch applies without conflict, you got now a branch for which you can do your pull request.

In a second step, you now need to decide what to do with your feature branch. If you haven't published your changes on this branch yet, the best procedure is probably rebasing this branch upon new-branch (and removing the last commit, if this is not done automatically by git rebase).

一抹苦笑 2024-10-28 01:51:46

我最终遇到了这样的情况:我分叉了一个分叉,并想将拉取请求提交回原始项目。

我有:

  • orignal_project
  • forked_project(从SHA:9685770的原始项目创建)
  • my_fork(从SHA:207e29b的分叉项目创建)
  • 我的分叉(SHA:b67627b)中的一个提交,我想将其提交回原始项目

为此,我:

  1. 从原始项目分叉的 SHA 创建一个新分支
  2. 从原始项目中提取所有内容
  3. 樱桃挑选了我想要作为拉取请求提交的提交
  4. 将其全部推送到 github

git 命令类似于:

  1. gitbranch my-feature -request 9685770
  2. git checkout my-feature-request
  3. git pull https://github.com/original_project/original_project.git
  4. gitcherry-pick b67627b
  5. git Push origin my-feature-request

然后我选择 my-feature-request 作为对原始项目的拉取请求的分支。

I ended up in a situation where I had forked a fork and wanted to submit a pull request back to the original project.

I had:

  • orignal_project
  • forked_project (created from original project at SHA: 9685770)
  • my_fork (created from forked project at SHA: 207e29b)
  • a commit in my fork (SHA: b67627b) that I wanted to submit back to original project

To do this, I:

  1. created a new branch from the SHA where the original project was forked
  2. pulled all from the original project
  3. cherry picked the commit I wanted to submit as a pull request
  4. pushed it all up to github

The git commands were something like:

  1. git branch my-feature-request 9685770
  2. git checkout my-feature-request
  3. git pull https://github.com/original_project/original_project.git
  4. git cherry-pick b67627b
  5. git push origin my-feature-request

Then I picked my-feature-request as the branch for my pull request to the original project.

三月梨花 2024-10-28 01:51:46

这几乎对我有用:

git checkout -b upstream upstream/master

git cherry-pick <SHA hash of commit>

git push origin upstream

唯一的区别是:

git push origin upstream:upstream

我需要更改最后一行,以便 git Push 在我的 GitHub 存储库中创建上游分支,以便我可以从中制作 PR。

This almost worked for me:

git checkout -b upstream upstream/master

git cherry-pick <SHA hash of commit>

git push origin upstream

The only difference was this:

git push origin upstream:upstream

I needed to change that last line so that git push would make the upstream branch in my GitHub repo so that I could make PR from it.

惟欲睡 2024-10-28 01:51:46

我已经进行了提交,我希望能够将其作为拉取请求隔离回当前分支。

所以我检查了一个新的分支

git checkout -b isolated-pull

这是我的解决方案与@Kevin Hakanson的不同的地方,因为我需要将此分支重置到历史记录中我想要区别的位置

git reset --hard [sha-to-diff-by]

并挑选来自的提交我想创建一个独立的拉取请求

git cherry-pick [my-isolated-commit-sha]

最后将其推送到远程

git push origin isolated-pull

并拉取请求数据。

I had already made the commit which I wanted to be able to isolate as a pull request back onto the current branch.

So I checked out a new branch

git checkout -b isolated-pull

And here's where my solution differs from @Kevin Hakanson's, as I need to reset this branch to the place in the history I want to diff from

git reset --hard [sha-to-diff-by]

And cherry-pick the commit from which I want to create an isolated pull request

git cherry-pick [my-isolated-commit-sha]

Finally push it up to the remote

git push origin isolated-pull

And pull request dat shi.

眼泪也成诗 2024-10-28 01:51:46

创建新的(临时)分支、挑选并为该分支创建拉取请求的解决方案并不令我满意。我不想更改我的存储库以使一组提交可用,因此我提出了以下替代方案:

首先为所有感兴趣的提交创建补丁文件:

git format-patch -1 <sha>

如果感兴趣的提交恰好是您可以使用的最后一个提交< code>HEAD 而不是

现在,您可以将补丁发送给源存储库的维护者,他们可以应用它们:

git branch new-branch <master or some older commit where the fork diverged>
git checkout new-branch

git am < <the patch>
...

git checkout master
git merge new-branch

最后,这应该看起来与通过拉取请求合并临时分支相同,但在分叉存储库中没有该附加分支。

The solution to create a new (temporary) branch, cherry-pick and creating the pull request for that branch did not satisfy me. I did not want to change my repository to make a set of commits available, so I came up with the following alternative:

First create patch files for all commits of interest:

git format-patch -1 <sha>

If the commit of interest happens to be the last one you can use HEAD instead <sha>.

Now, you can send the patches to the maintainer of the source repository, who can apply them:

git branch new-branch <master or some older commit where the fork diverged>
git checkout new-branch

git am < <the patch>
...

git checkout master
git merge new-branch

Finally this should look the same as if a temporary branch was merged by a pull request, but without having that additional branch in the fork-repository.

她说她爱他 2024-10-28 01:51:46

根据 @kevin-hakanson 的回答,我编写了这个小 bash 脚本来使这个过程更容易。如果上游存储库尚不存在,它将添加它(提示您输入 URL),然后提示您输入要创建的新分支的名称以及选择该分支的提交的标签/SHA。它会检查您当前所在的分支或提交,然后隐藏所有更改,以便您可以签出新分支。合并策略保留精选提交中的更改。将新分支推送到origin(假设是远程存储库的名称)后,您之前所在的分支或提交将再次检出,并且您之前的更改会从存储中弹出。

if ! git remote | grep -q upstream; then
    read -p "Upstream git repo URL: " upstream
    git remote add upstream $upstream
    git remote update
fi

read -p "Feature branch name: " feature_branch
# note: giving "master" is the same as giving the SHA it points to
read -p "SHA of commit to put on branch: " sha

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "HEAD" ]; then
    # detached HEAD; just get the commit SHA
    current_branch=$(git rev-parse --short HEAD)
fi
git stash
git checkout -b $feature_branch upstream/master
git cherry-pick --strategy=recursive -X theirs $sha
git push origin $feature_branch
git checkout $current_branch
git stash pop

(这在一些简单的测试中对我有用,但我不是 bash 程序员或 git 专家,所以如果我错过了一些可以更好地自动化的情况,请告诉我!)

Based on @kevin-hakanson's answer, I wrote this little bash script to make this process easier. It will add the upstream repo if it doesn't already exist (prompting you for the URL) then prompt for both the name of the new branch to create and the tag / SHA of the commit to cherry pick onto that branch. It checks what branch or commit you're currently on then stashes any changes so you can checkout the new branch. The merge strategy keeps the changes from the cherry-picked commit. After pushing the new branch to origin (assumed to be the name of your remote repo), the branch or commit you were on before is checked out again and your previous changes popped from the stash.

if ! git remote | grep -q upstream; then
    read -p "Upstream git repo URL: " upstream
    git remote add upstream $upstream
    git remote update
fi

read -p "Feature branch name: " feature_branch
# note: giving "master" is the same as giving the SHA it points to
read -p "SHA of commit to put on branch: " sha

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "HEAD" ]; then
    # detached HEAD; just get the commit SHA
    current_branch=$(git rev-parse --short HEAD)
fi
git stash
git checkout -b $feature_branch upstream/master
git cherry-pick --strategy=recursive -X theirs $sha
git push origin $feature_branch
git checkout $current_branch
git stash pop

(This has worked for me in a couple of simple tests, but I'm not a bash programmer or git expert, so let me know if there are cases I've missed that could be automated better!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文