如何在Git中重新定义同一分支的提交?

时间:2013-11-20 09:19:02

标签: git git-interactive-rebase

我在Git中做了一些错误的变基和冲突解决,所以我最终陷入了这种愚蠢的境地:

*   8379515 (HEAD, origin/master, origin/HEAD, master) Merge branch 'binary-mobile'
|\
| * 21b247a (binary-mobile) Add Binary Mobile
| * c66dced Add Music Collection
* | cbbe364 Add task Music Collection
|/
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

我的问题是提交Add task Music CollectionAdd Music Collection是一样的。我想:

  • 让我的分支遵循单一路径;
  • 只有一个标题为Add Music Collection的提交。

我可以git rebase -i 8e9ccae然后将c66dced压缩到cbbe364并将cbbe364重命名为Add Music Collection吗?我认为问题首先在于我已经将我想要修改的提交推送到Github,但我认为只要没有其他人在使用它们,这是可以的吗?

3 个答案:

答案 0 :(得分:2)

如果我遇到这个问题,我会做以下事情:

1。)将cbbe364(不包括)和21b247a(包括)之间的提交重新定位到8e9ccae

git rebase --onto 8e9ccae cbbe364 21b247a

这会将提交c66dced21b247a直接移至8e9ccae。这将导致最新提交添加二进制移动的另一个哈希值,让我们调用哈希值'commit2' (树可能不是100%准确,没有git存储库来重现它)

*   8379515 (HEAD, origin/master, origin/HEAD, master) Merge branch 'binary-mobile'
|\
| * 21b247a (binary-mobile) Add Binary Mobile
| * c66dced Add Music Collection
| | cbbe364 Add task Music Collection
|/
|
|
| * commit2 (HEAD) Add Binary Mobile
| * commit1 Add Music Collection
|/
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

2。)之后将master向下移动(沿着“树”向下移动到commit2

git branch -d master
git checkout commit2
git branch master

| * commit2 (HEAD, master) Add Binary Mobile
| * commit1 Add Music Collection
|/
* 8e9ccae Update README.md

3。)现在,您拥有master所属的地方,即commit2。 如果你想在'单行'上关注你的分支,你需要摆脱binary-mobile分支,只需删除它:

git branch -D binary-mobile

因为提交21b247a现在没有分支名称,并且您不在此分支上工作(没有HEAD),分支将消失。

4.)最后推动你的改变(在这种情况下强制是必要的)。这会将origin/master向下移动到master,合并分支应该消失。

git push master --force


* commit2 (HEAD, master, origin/master) Add Binary Mobile
* commit1 Add Music Collection
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

答案 1 :(得分:1)

如果我理解正确,你只需要检查并撤消你做错了什么。

使用git reflogSee here),您可以查看使用git存储库创建的所有内容的历史记录。所有这些更改都存储在git中,因此您可以返回到存储库过去2个月的任何状态。

使用 git reset --hard HEAD@{NumberOfTheChange}See here)您可以撤消存储库中的任何更改。 (但请注意,这样你就无法撤消文件中的更改,而这些更改未被git跟踪)

在此之后,你可以做你想做的事。

答案 2 :(得分:1)

我会做类似的事情:

  • 回到提交正常的地方
  • 在那里创建一个分支,并挑选你想要的提交
  • git重置到分支的起始点,这将成为您的新主/头,但您保存了inbetween提交
  • 合并分支并完成! :)

所以在代码中应该是这样的:

git checkout 8e9ccae
git checkout -b better_changes
git cherry-pick c66dced
git cherry-pick 21b247a
git checkout master
git reset --hard HEAD^6  //check correct nr of steps with git reflog
git merge better_changes

然后强制推动原点:)

相关问题