简体   繁体   English

合并后的git分支还原

[英]git branch restoration after merge

If I merge a git feature branch back onto master, is it best to then delete the branch? 如果我将git feature分支合并回master,最好再删除该分支?

Let's say 6 months later, I want to resurrect that deleted branch (assume a freshly cloned repo). 假设6个月后,我想复活那个删除的分支(假设是一个新克隆的仓库)。 Is there a way to easily re-create it or find its history assuming it was merged back onto master? 假设已将其合并回主数据库,是否可以轻松地重新创建它或查找其历史记录? What is the best way to do this? 做这个的最好方式是什么?

From reading other questions, I see that if I can find the head of that branch I can re-create it easily, but how do I go about doing that? 通过阅读其他问题,我发现如果可以找到该分支的头,就可以轻松地重新创建它,但是我该怎么做呢? Some other Q&A responses reference the "reflog" command, but this doesn't return anything useful if I have freshly cloned the repo. 其他一些Q&A响应引用了“ reflog”命令,但是如果我是新克隆回购协议的话,这不会返回任何有用的信息。

Whether or not it's best to delete a branch once merged depends on what the branch means to your workflow. 合并后最好删除分支取决于分支对您的工作流程的意义。 If it's a branch that's being used to prepare a new release, which gets merged into master once the release is ready, then you'll want to keep it around. 如果它是用于准备新发行版的分支,一旦发行版准备就绪,它将被合并到master中,那么您将需要保留它。 If it's a throwaway branch for developing a single feature, then it's usually preferable to delete it so you don't clutter your git branch output with dozens of completed branches. 如果它是用于开发单个功能的一次性分支,那么通常最好将其删除,这样您就不会使git branch输出中包含数十个已完成的分支。

If you want to re-create a deleted branch later, all you'll need to do is find the SHA that corresponds to its last commit. 如果您想稍后重新创建已删除的分支,则只需找到与其最后一次提交相对应的SHA。 If you don't have the reflog handy, you'll have to try to find this with git log . 如果您没有方便的reflog,则必须尝试使用git log找到它。 The easiest way to do this would be to search for the merge commit with something like: 最简单的方法是搜索合并提交,例如:

# This should match the message of the merge commit, if you kept the default.
git log --grep=branch-name

Once you find the merge commit, you'll see two parents listed: 找到合并提交后,您会看到列出了两个父母:

commit 38bf1d168e73f9fa708c334e50f256578d5c2d8f
Merge: a08b280 d7725b0

The first parent is the state of master before the merge; 第一父级是合并之前master节点的状态; the second is the state of the branch that was merged in, which is what you want. 第二个是合并分支的状态,这就是您想要的。 Recreate your branch there and check it out with: 在此处重新创建分支,并使用以下命令进行签出:

git checkout -b branch-name d7725b0

As a sidenote, this will only work if the merge wasn't a fast-forward merge . 附带说明一下,这仅在合并不是快速合并的情况下才有效 A fast-forward merge just zips HEAD up to the ref that's being merged, so it doesn't create a merge commit. 快速合并仅将HEAD到要合并的ref上,因此它不会创建合并提交。 You can force git to always make a new commit when you merge by using: 您可以使用以下命令强制git在合并时始终进行新提交:

git merge --no-ff branch-name

And if you always want this to be the case you can set it in your config: 而且,如果您始终希望如此,可以在配置中进行设置:

git config branch.master.mergeoptions  "--no-ff"

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

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