Git 提交新的pull request而不包含旧的commits

Git 提交新的pull request而不包含旧的commits

在本文中,我们将介绍如何使用Git和Github进行pull request操作,但是在创建新的pull request时不包含旧的commits。通常情况下,当我们向一个项目贡献代码时,我们会fork项目并创建一个新的分支,然后提交我们所做的更改。然而,有时我们只想提交新的commits,而不包含在旧的commits中。

阅读更多:Git 教程

什么是Git和Github?

首先,我们需要了解Git和Github的基本概念。Git是一个分布式版本控制系统,可以用于跟踪文件的更改并协同开发项目。Github是一个基于Git的代码托管平台,提供了一系列的协作工具,使得多人可以共同参与项目开发并提交pull request。

为什么我们不想包含旧的commits?

有时候,在向一个项目提交pull request时,我们不希望包含旧的commits。这可能是因为我们之前的commits包含了一些测试代码、调试信息或者其他不必要的更改。如果我们直接在新的分支上提交新的commits,那么旧的commits将会被包含在pull request中,给项目维护者带来额外的工作。因此,我们希望仅包含我们所做的新的commits,使得pull request更加干净和易于审查。

如何提交新的pull request而不包含旧的commits?

在这里,我们将介绍两种方法来提交新的pull request而不包含旧的commits。

方法一:使用rebase操作

第一种方法是使用Git的rebase操作。下面是具体的步骤:

  1. 确保你正在使用的是你fork的项目的最新版本。使用以下命令将项目的upstream添加为remote:
git remote add upstream [原始项目的URL]
Bash
  1. 从upstream拉取最新的更改:
git pull upstream [原始项目分支名称]
Bash
  1. 切换到你的分支:
git checkout [你的分支名称]
Bash
  1. 使用rebase操作将你的分支与upstream分支合并,同时忽略旧的commits:
git rebase -i upstream/[upstream分支名称]
Bash
  1. 进入rebase页面后,你可以选择保留、取消或者修改每个commit。如果你只想保留最新的commit,可以将前面的commit的命令改为”pick”,并将最新的commit的命令改为”edit”。保存并关闭页面。
  2. Git会自动切换到每个commit的编辑模式。你可以使用以下命令修改每个commit,或者直接跳过某个commit:
git commit --amend   # 修改commit
git rebase --skip    # 跳过commit
Bash
  1. 完成所有修改后,使用以下命令强制推送到你的远程分支:
git push --force origin [你的分支名称]
Bash
  1. 在Github上创建新的pull request,并确保只包含你所需要的新的commits。

方法二:使用cherry-pick操作

第二种方法是使用Git的cherry-pick操作。下面是具体的步骤:

  1. 确保你正在使用的是你fork的项目的最新版本。使用以下命令将项目的upstream添加为remote:
git remote add upstream [原始项目的URL]
Bash
  1. 从upstream拉取最新的更改:
git pull upstream [原始项目分支名称]
Bash
  1. 切换到你的分支:
git checkout [你的分支名称]
Bash
  1. 创建一个新的分支,用于仅包含新的commits:
git checkout -b [新的分支名称]
Bash
  1. 使用cherry-pick操作将你所需要的新的commits从你的原始分支中导入到新的分支:
git cherry-pick [commit1] [commit2] ...
Bash
  1. 完成所有cherry-pick操作后,使用以下命令推送到你的远程分支:
git push origin [新的分支名称]
Bash
  1. 在Github上创建新的pull request,并确保只包含你所需要的新的commits。

示例说明

为了更好地理解这两种提交新的pull request而不包含旧的commits的方法,我们来看一个示例:

假设我们要向一个名为”awesome-project”的项目提交一个新的pull request,我们的fork项目地址为”https://github.com/your-username/awesome-project”,我们创建的新的分支名称为”new-feature”。在这个示例中,我们已经更新并提交了三个commits到我们的新的分支”new-feature”,但我们不想包含之前的两个commits。

使用rebase操作的示例

我们使用方法一中的rebase操作来提交新的pull request而不包含旧的commits。

  1. 添加upstream remote:
git remote add upstream https://github.com/original-username/awesome-project.git
Bash
  1. 拉取upstream最新更改:
git pull upstream master
Bash
  1. 切换到新的分支”new-feature”:
git checkout new-feature
Bash
  1. 使用rebase操作忽略前两个commits:
git rebase -i upstream/master
Bash

修改rebase页面如下:

pick commit1     # 保留commit1
edit commit2     # 忽略commit2
edit commit3     # 保留commit3
Bash

保存并关闭页面。

  1. 修改commit2:
git commit --amend
Bash

在编辑模式中,修改commit2的内容。保存并关闭页面。

  1. 完成修改后,强制推送到远程分支:
git push --force origin new-feature
Bash
  1. 在Github上创建新的pull request,并确保只包含commit1和commit3。

使用cherry-pick操作的示例

我们使用方法二中的cherry-pick操作来提交新的pull request而不包含旧的commits。

  1. 添加upstream remote:
git remote add upstream https://github.com/original-username/awesome-project.git
Bash
  1. 拉取upstream最新更改:
git pull upstream master
Bash
  1. 切换到新的分支”new-feature”:
git checkout new-feature
Bash
  1. 创建一个新的分支”new-commits”:
git checkout -b new-commits
Bash
  1. 使用cherry-pick操作导入commit1和commit3:
git cherry-pick commit1 commit3
Bash
  1. 推送到远程分支:
git push origin new-commits
Bash
  1. 在Github上创建新的pull request,并确保只包含commit1和commit3。

总结

本文介绍了如何提交新的pull request而不包含旧的commits。我们探讨了两种方法,一种是使用rebase操作,另

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册