Git - 移动提交到一个新的分支(重命名分支,而不是强制推送?) [英] Git - move pushed commits to a new branch (rename branch instead of force pushing?)

查看:170
本文介绍了Git - 移动提交到一个新的分支(重命名分支,而不是强制推送?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有另外一个问题几乎完全描述了我的情况( Git - 推动提交到不同的分支)。唯一的区别是我想移动的提交在Develop分支而不是Master(这不应该因为主分支没什么特别之处)。此外,我们有redmine链接到我们的git存储库。无论如何,用户Adam对已接受的答案有一个评论:


不要使用push -force,这是一个糟糕的主意。它会严重破坏已经拉过的任何其他用户的历史记录。更糟糕的是,如果你的Git日志集成到其他系统中,比如Redmine,Jira等,那么它会严重破坏数据库并且很难清理。


我想回复评论以要求澄清,但我刚刚为此问题创建了一个帐户,并没有足够的信誉点发表评论。在我的情况下,只有一个其他开发人员在项目中工作,所以我并不担心会破坏其他人的历史。我关心的是关于'更糟'的情况应该怎么办?我希望在我的情况下可能会没问题,因为在我之后没有任何红宝石问题需要移动的第一个提交(在上面引用的提交中提交C)。我不确定,因为我不知道如果一个版本库的历史被重写,那么所有可能会在redmine中搞砸的。


我正在考虑它,因为我正在寻找答案,我想我可能有另一种解决方案(虽然我没有足够的信心将我的git知识作为答案发布)。如果我将Develop重新命名为NewBranch,然后从提交C创建一个新的Develop分支?

git branch -m开发NewBranch#在本地重命名
git push origin:开发NewBranch#删除远程开发分支& push NewBranch
git push origin -u NewBranch#重置上游分支

#create new开发分支
git checkout -b开发$ SHA1_OF_C
git push -u原点开发

看起来这样可行。有什么问题吗?历史将会是什么样子?这会如何影响redmine(除了可能需要将默认分支更改为新Develop)?我认为它看起来与我想要的完全相同,只不过它会显示Develop从NewBranch分支而不是其他方式。

解决方案

我的建议是 cherry-pick 您提交到新分支,而不是重命名现有分支。



然后,你将能够 push 分支到远程而不需要强制推送它。

  git分支-b BRANCHNAME#创建新分支
git cherry-pick HASHVALUE #Cherry逐个挑选所有提交。 (例如:HASHVALUE - 5e2ld9)


There is another question that describes my situation almost exactly (Git - Moving Pushed Commits to a Different Branch). The only difference is that the commits that I would like to move are in the Develop branch rather than master (which shouldn't matter because there's nothing special about the master branch). Also, we have redmine linked to our git repositories. Anyway, there was a comment by user Adam to the accepted answer:

Don't ever use push --force, it's a bad idea. It will seriously corrupt history on any other users who have already pulled. Even worse, if you have your Git log integrated into other systems such as Redmine, Jira, etc then it can seriously corrupt the databases and be quite difficult to clean up.

I wanted to reply to the comment to ask for clarification, but I had just created an account for this problem and don't have enough reputation points to post a comment. In my situation, there is only one other developer working on the project, so I'm not worried about corrupting the history of others. The thing I'm concerned about is what should be done about the 'even worse' case? I'm hoping that it might be okay in my situation because there haven't been any redmine issues entered after the first commit that needs to be moved (commit C in the above referenced post). I'm not sure though, because I don't know what all could get screwed up in redmine if a repository's history is rewritten.

I was thinking about it as I was searching for an answer, and I think I might have an alternative solution (I'm not confident enough in my git knowledge to post it as an answer, though). What if I rename Develop to NewBranch and then create a new Develop branch off of commit C?

# rename branch
git branch -m Develop NewBranch     # Rename locally
git push origin :Develop NewBranch  # Delete remote Develop branch & push NewBranch
git push origin -u NewBranch        # Reset upstream branch

#create new Develop branch
git checkout -b Develop $SHA1_OF_C
git push -u origin Develop

It seems like this would work. Are there any gotchas? What would the history look like? How would this affect redmine (aside from maybe having to change the default branch to the new Develop)? I think that It would look identical to what I want, except that it would show Develop branching off of NewBranch instead of the other way around.

解决方案

My suggestion is to cherry-pick your commits to a new branch rather than renaming the existing branch.

Then you will be able to push that branch to remote without force push it.

git branch -b BRANCHNAME #Create new branch
git cherry-pick HASHVALUE #Cherry pick all the commits one by one. (eg: HASHVALUE - 5e2ld9)

这篇关于Git - 移动提交到一个新的分支(重命名分支,而不是强制推送?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆