简体   繁体   English

Git rebase将当前分支留在远程后面,想要更清晰的提交历史记录

[英]Git rebase leaves current branch behind remote, want a cleaner commit history

I upload commits from my local Git repo to GitHub regularly, but it was overloaded with commits, and after deciding they are not useful and just spoil the history, I tried using rebase -i to squash some of the commits together. 我将提交内容从本地Git存储库定期上传到GitHub,但是提交内容过载,并且在确定它们无用并且破坏了历史记录之后,我尝试使用rebase -i将一些提交内容压缩在一起。

It seemed to work, so I wanted to push the changed commits to GitHub and change the history there. 它似乎可行,所以我想将更改的提交推送到GitHub并在此处更改历史记录。 What I get is that the tip of my current branch is behind. 我得到的是我当前分支的尖端在后面。 And the suggested action is to pull changes from the remote repo, but I don't want the changes from the remote. 建议的操作是从远程存储库中提取更改,但是我不希望从远程存储中获取更改。 I just want to send what I have locally to GitHub. 我只想将本地拥有的内容发送到GitHub。

Can you please explain me the situation a bit and help to achieve what I want? 您能给我解释一下情况,帮助实现我想要的吗?

To edit some pushed branches is a no-go, however you can do it. 编辑某些推送的分支是不可行的,但是您可以这样做。

maybe your commit log looks like this: 也许您的提交日志如下所示:

o 250a32c added new information <master> <origin/master>
|
o c4f5265 added new information
|
o 168d674 added new information
|
o ca708cd some other commit

You can now with interactive rebase squash some commits. 现在,您可以使用交互式变基压缩一些提交。

git rebase -i ca708cd

An editor opens and you can change the pick before the commits 250a32c and c4f5265 to squash . 将打开一个编辑器,您可以在将250a32cc4f5265提交到squash之前更改pick

After this, your repo looks like this: 之后,您的仓库如下所示:

o 3e86bc4 added new information <master>
| o 250a32c added new information <origin/master>
| |
| o c4f5265 added new information
| |
| o 168d674 added new information
|/
o ca708cd some other commit

Now you have to need to push your branch to the github with --force , because your master and master/origin are out of sync and cannot be fast-forwarded. 现在,您需要使用--force将分支推送到github,因为您的master和master / origin不同步,并且无法快速转发。

git push --force origin master

Rewriting history after you have published commit publicly is not a very good thing to do. 在公开发布提交后重写历史记录不是一件好事。 Imagine someone else cloned your repository. 想象其他人克隆了您的存储库。 If you change it afterwards, their history wouldn't match their origin (your github repo). 如果事后更改它,它们的历史记录将与它们的来源不符(您的github存储库)。

You have several solutions, you can: 您有几种解决方案,可以:

  • try and go on and change the repo on github. 尝试继续并更改github上的仓库。 One possibility is to completely discard the branches you don't like on github, and then push your arranged local branches to github (silvio's "--force" being a faster alternative). 一种可能是完全丢弃您在github上不喜欢的分支,然后将您安排好的本地分支推送到github(silvio的“ --force”是一种更快的选择)。

  • remove the repo completely and clone your local repo to github again. 完全删除该存储库,然后再次将本地存储库克隆到github。

  • live with the not so beautiful commits (preferred solution). 与不太漂亮的提交一起生活(首选解决方案)。

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

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