[英]How to get the lastest commit from branch A that was the leave when branch B was merged into A?
I need to checkout a version of my project locally so I can work on features that existed before branch B was merged into A, so I need to work with latest commit from A just before B was merged.我需要在本地签出我的项目的一个版本,以便我可以处理分支 B 合并到 A 之前存在的功能,所以我需要在 B 合并之前处理来自 A 的最新提交。
Thanks in advance.提前致谢。
Edit编辑
Suppose we have commit 1 on the branch A and on branch B we have commit 2,3,4,5...n.假设我们在分支 A 上有提交 1,在分支 B 上有提交 2,3,4,5...n。 As the branch wasn't merged until som time later, some other pr were merged into A, so we have now on A commits 1,n+1,...Y.由于分支直到一段时间后才合并,其他一些 pr 被合并到 A 中,所以我们现在在 A 上提交了 1,n+1,...Y。
Now we will merge branch B into A and keep the work going until some months later.现在我们将分支 B 合并到 A 并继续工作直到几个月后。
First question is: Will A stays as 1,n+1,...Y,2,3,4,5...n after the merge?第一个问题是:合并后 A 会保持为 1,n+1,...Y,2,3,4,5...n 吗?
Main question: How can I get commit 1 if I don't now it's id or commit message?主要问题:如果我现在不是 id 或提交消息,我如何获得提交 1?
You can do a git pull
to make sure your project is up-to-date with the repository.您可以执行git pull
以确保您的项目与存储库保持同步。 Now grab the commit id from before B was merged (we'll call it commit-id) and execute following commands:现在从 B 合并之前获取提交 id(我们称之为 commit-id)并执行以下命令:
$git branch my-new-branch //This will create a new branch
$git branch -f my-new-branch commit-id //This will point your local branch to the new commit
Use cherry pick to bring changes in to current branch from another branch使用cherry pick将更改从另一个分支引入当前分支
You can check out any historical commit, as a detached HEAD .您可以检查任何历史提交,作为分离的 HEAD 。 This uses a special mode in Git, one where you view a commit but normally don't actually do any new work.这在 Git 中使用了一种特殊模式,您可以在其中查看提交,但通常不会实际执行任何新工作。
To do this, find the commit's raw hash ID, or use any Git tag , which is mostly a way of putting a human-readable name in to say this name shall, from now until the true death of this project, mean hash ID _____ (fill in the blank).为此,请找到提交的原始 hash ID,或使用任何 Git标记,这主要是一种输入人类可读名称的方式,表示该名称应从现在到该项目真正死亡,意思是 Z0804052577294C34E (填空)。 Use this hash ID or human-readable name as an argument to git checkout
or git switch
.使用此 hash ID 或人类可读名称作为git checkout
或git switch
的参数。 If you're using a raw hash ID, you can abbreviate as long as it is not ambiguous, but the easy way to get it right—raw hash IDs are very unfriendly to humans, who always 1 get them wrong—is to cut-and-paste the hash ID from some git log
output for instance:如果您使用的是原始 hash ID,您可以缩写,只要它不模棱两可,但正确的简单方法是正确的 - 原始 hash ID 对人类非常不友好,他们总是1弄错了 - 就是剪掉 -并从一些git log
output 中粘贴 hash ID,例如:
git checkout 86ab15cb154862b6fa5cc646dac27532f881e1fb
When you are working with a Git repository, you normally work with one where Git has prepared a working tree or work-tree area for you.当您使用 Git 存储库时,您通常使用 Git 为您准备好工作树或工作树区域的存储库。 This git checkout
fills in your work-tree from the commit you specify, which becomes the current commit.这个git checkout
出从您指定的提交中填充您的工作树,该提交成为当前提交。
(If you have Git 2.23 or later, you may wish to use git switch
instead of git checkout
here. It does exactly the same thing, for this case.) (如果您有 Git 2.23 或更高版本,您可能希望在此处使用git switch
而不是git checkout
。对于这种情况,它的作用完全相同。)
1 Insert distorted Gilbert-&-Sullivan routine here. 1在此处插入扭曲的 Gilbert-&-Sullivan 例程。 "What, never? No, always." “什么,从来没有?不,总是。” or something like that.或类似的东西。
In normal everyday Git usage, you use something like:在正常的日常 Git 使用中,您使用以下内容:
git checkout master # or git switch master
or:或者:
git checkout develop # or git switch develop
where you supply a branch name .在哪里提供分支名称。 The branch name selects a commit (by its hash ID as always) and Git fills in your work-tree from that commit, but instead of just picking a commit, this also tells Git: ... and when I make my next commit, add it to the branch I am using.分支名称选择一个提交(通过它的 hash ID 一如既往)和 Git 从该提交中填充您的工作树,但不仅仅是选择一个提交,这也告诉 Z0BCC70105AD279503E651FE7B3F4B和何时提交我的下一个提交......将其添加到我正在使用的分支中。
When you use:当您使用:
git checkout <raw hash ID>
you're still selecting a commit, but promising Git that if you do make later commits, they're all temporary and can all be thrown away later.您仍在选择提交,但向 Git 承诺,如果您稍后提交,它们都是临时的,以后都可以丢弃。 So you can use the work-tree files all you like, for viewing or compiling or whatever, but you don't normally actually make any new commits.因此,您可以随意使用工作树文件,用于查看或编译或其他任何内容,但实际上您通常不会进行任何新的提交。
(If you do make new commits in this mode, you get an opportunity to tell Git oh, and keep these around now , as long as you do it reasonably quickly—within a month or so, by default—of re-attaching HEAD
to a branch, or switching to another commit. Git also uses this detached HEAD mode internally for some of its own operations, such as doing git rebase
.) (如果你确实在这种模式下进行了新的提交,你有机会告诉 Git哦,现在就保留这些,只要你在一个月左右的时间内合理地快速完成,默认情况下 - 将HEAD
重新附加到一个分支,或者切换到另一个提交。Git 也在内部使用这种分离的 HEAD 模式进行一些自己的操作,例如做git rebase
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.