Git - 只推送对 github 的最新提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2276686/
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-19 04:05:28  来源:igfitidea点击:

Git - only push up the most recent commit to github

gitgithub

提问by Dave Mateer

On my local git repo I've got many commits, which include 'secret' connection strings :-)

在我的本地 git repo 上,我有很多提交,其中包括“秘密”连接字符串:-)

I don't want this history on github when I push it there.

当我将其推送到 github 时,我不希望在 github 上显示此历史记录。

Essentially I want to push everything I have, but want to get rid of a whole lot of history.

本质上我想推动我拥有的一切,但想摆脱很多历史。

Perhaps I would be better running in a branch for all my dev, then just merging back to master before committing... then the history for master will just be the commit I want.

也许我会更好地为我的所有开发人员在一个分支中运行,然后在提交之前合并回 master ......然后 master 的历史将只是我想要的提交。

I've tried running rebase:

我试过运行 rebase:

git rebase –i HEAD~3

That went back 3 commits, and then I could delete a commit.

那返回了 3 个提交,然后我可以删除一个提交。

However ran into auto cherry-pick failed, and it got quite complex.

然而,遇到自动樱桃挑选失败,它变得非常复杂。

Any thoughts greatly appreciated... no big deal to can the history and start again if this gets too hard :-)

任何想法都非常感谢......如果这太难了,历史和重新开始没什么大不了的:-)

回答by Greg Hewgill

You can branch your current work, rewind the master, then cherry-pick the latest commit back to the master:

你可以分支你当前的工作,回退主,然后挑选最新的提交回到主:

git branch secret
git reset --hard HEAD~3
git cherry-pick secret

In pictures,

在图片中,

    A--B--C--D--E (master)

after git branch secret:

之后git branch secret

    A--B--C--D--E (master, secret)

after git reset --hard HEAD~3:

之后git reset --hard HEAD~3

    A--B (master)
        \
         C--D--E (secret)

after git cherry-pick secret:

之后git cherry-pick secret

    A--B--E' (master)
        \
         C--D--E (secret)

Finally, if you git checkout secret; git rebase master, you can get:

最后,如果你git checkout secret; git rebase master,你可以得到:

    A--B--E' (master)
           \
            C--D (secret)

回答by Paschalis

Scenario

设想

You cloned a project and made a ridiculous amount of commits for a new feature, and the time has come to open source it or share it for review. And you don't want anyone to see your commit mess!

你克隆了一个项目并为一个新特性做了大量的提交,现在是时候开源它或共享它以供了。而且您不希望任何人看到您的提交混乱!

The original branch was cloned_branch, your messy commits were on devbranch, and you want to publish your clean work on the public_branchbranch.

原始分支是cloned_branch,您的杂乱提交在dev分支上,并且您想在该public_branch分支上发布您的干净工作。

Assume the following commits:

假设以下提交:

On branch cloned_branch:

在分行cloned_branch

SHA0: Implemented X, and Y. (author:authorA)
SHA1: Implemented Z. (author:authorA)
SHA2: Implemented W. (author:authorA)

SHA0:实现 X 和 Y。(作者:作者 A)
SHA1:实现 Z。(作者:作者 A)
SHA2:实现 W。(作者:作者 A)

You created devbranch and pushed some commits:

您创建了dev分支并推送了一些提交:

SHA3: trying to implement Q feature to work..
SHA4: shit, I doesn't work
SHA5: messed up even more! :(
SHA6: GOT IT WORKING!!!!
SHA7: cleanup!

SHA3​​:试图实现 Q 功能来工作..
SHA4:该死,我不工作
SHA5:更糟!:(
SHA6:开始工作了!!!!
SHA7:清理!

Put all these changes in a single commit in public_branch:

将所有这些更改放在单个提交中public_branch

git checkout SHA2 -b public_branch      # new branch at `authorA`'s last commit
git merge --squash SHA7        # squash commits up to your last one
git commit -m "Implemented Q (author:me)"

Result:

结果:

Branches cloned_branchand devremain as before, whilepublic_branchbranch contains:

分支cloned_branchdev保持原样,而public_branch分支包含:

SHA0: Implemented X, and Y. (author:authorA)
SHA1: Implemented Z. (author:authorA)
SHA2: Implemented W. (author:authorA)
SHA8: Implemented Q. (author:me)

SHA0:实施 X 和 Y。(作者:作者 A)
SHA1:实施 Z。(作者:作者 A)
SHA2:实施 W。(作者:作者 A)
SHA8:实施 Q。(作者:我)

回答by Marcpek

I don't have enough reputation to comment, so I'll post an answer.

我没有足够的声誉发表评论,所以我会发布一个答案。

If you have submodules, an approach similar to Paschalis' answer provides a more stable alternative (cherry-pickdoes not take submodules into account) and minimises conflicts (none in my case).

如果您有子模块,类似于 Paschalis 的答案的方法提供了更稳定的替代方案(cherry-pick不考虑子模块)并最大限度地减少冲突(在我的情况下没有)。

Here's my preferred approach:

这是我的首选方法:

git checkout OLDSHA -b temp
git merge --squash master
git commit -m "All my work"

Where OLDSHAis the oldest commit you feel safe to push to the public repository and masteris the branch that contains all your work.

OLDSHA您认为可以安全地推送到公共存储库的最旧提交在哪里,以及master包含您所有工作的分支。

This creates a new branch (temp), which "hides" (by squashing) all history between OLDSHAand the most recent commit on master. The message for the new commit will be "All my work".

这将创建一个新分支 ( temp),它“隐藏”(通过压缩) .OLDSHA和最近一次提交之间的所有历史记录master。新提交的消息将是“我所有的工作”。

You can then deletemaster and make temp the new master (before doing this, make sure master has been safely pushed to another repository or backed up)

然后,您可以删除master 并使 temp 成为新的 master(在此之前,请确保 master 已安全地推送到另一个存储库或已备份)

git branch -d master
git branch -m temp master

And push to a new remote, keeping your old remote intact

并推送到新遥控器,保持旧遥控器完好无损

git remote rm origin
git remote add origin NEW_URL
git push --set-upstream origin master