如何在不提交的情况下在 Git 中创建补丁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9810752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:16:35  来源:igfitidea点击:

How to create a patch without commit in Git

gitpatch

提问by doglin

I did some search online. I know that you can use format-patch after commit, but my situation is a little different.

我在网上做了一些搜索。我知道你可以在提交后使用 format-patch,但我的情况有点不同。

I want to create a patch, similar to "dpk" in SVN, so I can send it out for code review, but I don't yet want to commit it.

我想创建一个补丁,类似于 SVN 中的“dpk”,以便我可以将其发送给代码,但我还不想提交。

How can I achieve this with Git?

我如何使用 Git 实现这一目标?

采纳答案by hammar

Committing in Git is a cheap and entirely local operation, so there is no reason to avoid committing as long as you don't push it anywhere.

在 Git 中提交是一种廉价且完全本地化的操作,因此只要不将其推送到任何地方,就没有理由避免提交。

Just make a new local branch and commit your changes there. You can always delete the branch later if you don't want it anymore, or you can keep the branch and use it for working on whatever you're doing, then merge (or rebase) it into the master branch when it's ready. This is a good workflow to use when working with Git.

只需创建一个新的本地分支并在那里提交您的更改。如果您不再需要该分支,您可以随时删除它,或者您可以保留该分支并使用它来处理您正在做的任何事情,然后在它准备好时将它合并(或变基)到主分支中。在使用 Git 时,这是一个很好的工作流程。

$ git checkout -b feature-foo  # create and switch to new branch feature-foo
$ git commit

# do whatever you need to do

$ git checkout master          # switch back to the master branch
$ git merge feature-foo        # merge your change into master (optional)
$ git branch -d feature-foo    # delete the branch

回答by RayLuo

When other guys had already given some answer which comply with git convention, the OP's question, "create a patch without commit", can be also solved in this way:

当其他人已经给出了一些符合 git 约定的答案时,OP 的问题“无需提交即可创建补丁”也可以通过这种方式解决:

git diff > my_patch.txt

Later you can apply this patch, also without a commit, by:

稍后您可以通过以下方式应用此补丁,也无需提交:

git apply my_patch.txt

But if you are just working locally, a git checkout another_branch -mis good enough to bring all your current uncommit changes to that another_branch, without even patch and apply.

但是,如果您只是在本地工作,agit checkout another_branch -m足以将您当前所有未提交的更改带到该 another_branch,甚至无需修补和应用。

回答by Pintu Patel

general step to generate patch without commit at last

最后生成不提交补丁的一般步骤

  1. commit your local changes using

    git commit -a -m "specific message"
    

    Note : don't push this commit.

  2. generate patch

    git format-patch -s -n -1 HEAD   
    

    it will generate 0001-.patch

  3. revert back local commit

    git reset --soft HEAD~1
    

    to delete commit but keep your work

    git reset --hard HEAD~1
    

    to delete commit with your work

  1. 使用提交您的本地更改

    git commit -a -m "specific message"
    

    注意:不要推送此提交。

  2. 生成补丁

    git format-patch -s -n -1 HEAD   
    

    它将生成 0001-.patch

  3. 恢复本地提交

    git reset --soft HEAD~1
    

    删除提交但保留您的工作

    git reset --hard HEAD~1
    

    删除提交与您的工作

回答by manojlds

Like @hammar said, commit is cheap and then you can blow away the commit with git resetetc.

就像@hammar 说的那样,提交很便宜,然后你就可以用git reset等等来消除提交了。

You can also stash and then do:

您还可以隐藏然后执行以下操作:

git stash show -p

回答by mockobject

A commit to a local repo in git is not "binding". You can commit your changes, create your patch and then do a soft reset on your branch to the previous commit and it is like your commit never happened.

在 git 中提交到本地存储库不是“绑定”。你可以提交你的更改,创建你的补丁,然后在你的分支上对之前的提交进行软重置,就像你的提交从未发生过一样。

That being said, there really is no reason you HAVE to reset your branch after creating the patch. You can leave the commit in the repo and just avoid pushing it until the code review is done. If you have to go back and make changes to the original commit you have options at that point.

话虽如此,在创建补丁后,您真的没有理由必须重置您的分支。您可以将提交留在 repo 中,并在代码完成之前避免推送它。如果您必须返回并对原始提交进行更改,则此时您可以选择。

And if you create a branch for the commit as hammar suggests it makes it even easier to go back and make changes later without having to do any annoying rebasing and such in the main branch before pushing.

如果您按照 hammar 的建议为提交创建一个分支,则可以更轻松地返回并进行更改,而无需在推送之前在主分支中进行任何烦人的变基等操作。