简体   繁体   English

获取当前修改文件的 git patch

[英]Get git patch of current modified files

As a noob in git, i have no idea how to have patch to current modified files relative to original ones, ie the first git clone.作为 git 中的菜鸟,我不知道如何对当前修改过的文件相对于原始文件打补丁,即第一个 git 克隆。 Anyone experienced expert on this developmental software versioning, mainly patch and diff for working as collaborator (in fork, PR, etc) so that explain the correct way ?任何对此开发软件版本控制有经验的专家,主要是作为合作者(在 fork、PR 等中)工作的patch和差异,以便解释正确的方法?

Making a patch制作补丁

You can create a patch that represents all your uncommitted changes simply by saving the output of git diff to a file:只需将git diff的输出保存到文件中,您就可以创建一个表示所有未提交更改的补丁:

git diff > my-changes.patch

This patch is just a file, and so you could email it to someone or save it on a thumb drive or do whatever else you wanted to with it (I'm not sure why you'd need to do this, but you can)此补丁只是一个文件,因此您可以将其通过电子邮件发送给某人或将其保存在 U 盘上,或者使用它做任何您想做的事情(我不确定您为什么需要这样做,但您可以)

Then, the patch can be applied via然后,补丁可以通过

git apply my-changes.patch

You typically don't need to make patches manually, and when you do need to make patches, they should always be from pre-existing commits.您通常不需要手动制作补丁,当您确实需要制作补丁时,它们应该始终来自预先存在的提交。

A patch can be made from pre-existing commits like so:可以从预先存在的提交中制作补丁,如下所示:

git diff <start commit>..HEAD > my-changes.patch

This will create a patch of all changes since <start commit> .这将创建自<start commit>以来所有更改的补丁。 You can also specify <start commit> as a branch, a tag (which refers to a specific commit), or something specific like main~10 (which would mean the main branch, 10 commits ago).您还可以将<start commit>指定为分支、标签(指的是特定提交)或特定的东西,例如main~10 (这意味着主分支,10 次提交前)。

# Get a patch of all changes that haven't been pushed
git diff origin/main..main > local-changes.patch

# Get a patch of all changes on feature-branch
git diff main..feature-branch > changes-on-feature-branch.patch

# Get the last 10 commits as a patch
git diff main~10..main > last-10-commits.patch

# Get all the changes since a particular tag
git diff v1.0.0..main > changes-since-v1.0.0.patch

ONCE AGAIN, YOU TYPICALLY DO NOT NEED TO CREATE PATCHES MANUALLY!!!再一次,您通常不需要手动创建补丁!!! As a tool, git diff is really useful for exploring history, seeing current changes, seeing changes that will get merged, or a million other uses.作为一种工具, git diff对于探索历史、查看当前更改、查看将被合并的更改或一百万种其他用途非常有用。 It outputs stuff in a form that can be directly applied with git apply , but I haven't had to manually send someone a patch at any point in my entire life.它以一种可以直接使用git apply应用的形式输出内容,但我一生中的任何时候都不必手动向某人发送补丁。

Collaborating in git, the right way在 git 中协作,正确的方式

There are three typical ways to collaborate, which I'll list from easiest to most advanced.共有三种典型的协作方式,我将从最简单到最高级的顺序列出。

Method 1: Multiple collaborators, who all have access方法 1:多个协作者,谁都可以访问

This is the most typical situation you'll see in school, or in most work environments.这是您在学校或大多数工作环境中会看到的最典型情况。 Everyone can push code to the same repository.每个人都可以将代码推送到同一个存储库。

Once you have a copy of the repo on your machine, the workflow looks like this:在您的机器上拥有一份 repo 副本后,工作流程如下所示:

  • Make changes or updates to the code对代码进行更改或更新

  • Commit your changes: git add <your changes> && git commit提交您的更改: git add <your changes> && git commit

    You can make as many commits as you want before uploading.您可以在上传之前进行任意数量的提交。 The best thing to do is make small commits every time you finish an important piece of code, like a function, or a class.最好的做法是在每次完成重要的代码(例如函数或类)时进行小的提交。

  • Download remote changes: git pull下载远程更改: git pull

    If your partners or coworkers have updated the repository with changes, you have to do this before pushing.如果您的合作伙伴或同事已使用更改更新了存储库,则您必须在推送之前执行此操作。 This allows you to check to make sure that your changes work with their changes.这允许您检查以确保您的更改与其更改一起使用。

  • Upload your changes: git push上传您的更改: git push

Method 2: you're working on someone else's open source project方法二:你在做别人的开源项目

You won't always be able to push your changes directly.您不会总是能够直接推送您的更改。 For example, if you're working on an open source project, you'll typically submit changes for review, and the maintainers will be the ones to merge them.例如,如果您正在处理一个开源项目,您通常会提交更改以供审核,而维护者将是合并它们的人。

This is typically done by forking the project, making changes, uploading your changes to your repository (the forked version), and then submitting a pull request to integrate your changes back into the original project.这通常是通过分叉项目、进行更改、将更改上传到存储库(分叉版本),然后提交拉取请求以将更改集成回原始项目来完成的。

It looks like this:它看起来像这样:

  • Fork (on github, gitlab, or another service Fork(在 github、gitlab 或其他服务上
  • Clone the forked version onto your local machine将分叉版本克隆到本地机器上
  • Make changes做出改变
  • Commit your changes提交您的更改
  • Push your changes推送您的更改
  • Create a pull request to merge your changes back in (You can do this on github, gitlab, or wherever you forked the repo)创建一个拉取请求来合并你的更改(你可以在 github、gitlab 或任何你分叉 repo 的地方这样做)

Method 3: Email your commits as a patch方法 3:将您的提交作为补丁通过电子邮件发送

This method is typically used for larger, older open-source projects, that don't want to rely on a centralized service like github or gitlab.这种方法通常用于较大的、较旧的开源项目,它们不想依赖像 github 或 gitlab 这样的集中式服务。

It's done by using git send-email to email the patches directly from the command line.这是通过使用git send-email直接从命令行通过电子邮件发送补丁来完成的。 Git will create a patch for you automatically. Git 会自动为你创建一个补丁。 All you have to do is specify the range of commits you want to send as a patch.您所要做的就是指定要作为补丁发送的提交范围。

For example, if you made your changes on a local branch called dev , then you would use it like this:例如,如果您在名为dev的本地分支上进行了更改,那么您可以像这样使用它:

git send-email --compose --from=<your email> --to=<their email> master..dev

Here, master..dev is the range of patches you're sending.在这里, master..dev是您发送的补丁范围。 It's all the patches on the dev branch, that aren't on the master branch.这是dev分支上的所有补丁,而不是 master 分支上的补丁。 AKA, your changes. AKA,你的改变。

This command will open a text editor where you can write the body of your email, and git will attach the patch to the email, and send it.此命令将打开一个文本编辑器,您可以在其中编写电子邮件正文,git 会将补丁附加到电子邮件中,然后发送。

Note that you have to configure your email to work with git send-email.请注意,您必须配置您的电子邮件以使用 git send-email。 Beginners will not typically need to do this.初学者通常不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM