Git:获取分支上的最新提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/456552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Fetching latest commit on a branch
提问by Andrew
I'm struggling to understand something about GIT.
我正在努力了解有关 GIT 的内容。
We've got a repository with a number of branches in it. I can fetch a local copy of one of those branches (git checkout -b ...), make some changes, then push them back. If I fail to tag the end of the branch, however, how do other users get the head of the branch? WHen they use git checkout to fetch the branch they get the code at the point of the branch -- not the head of that branch.
我们有一个包含许多分支的存储库。我可以获取这些分支之一的本地副本(git checkout -b ...),进行一些更改,然后将它们推回。但是,如果我没有标记分支的末尾,其他用户如何获得分支的头部?当他们使用 git checkout 获取分支时,他们会在分支点获取代码——而不是该分支的头部。
What am I missing here?
我在这里缺少什么?
EDIT: I came into this project late and was involved in the CVS to GIT conversion. I am assuming that the FOO_3_4_0001... tags all mark points along the branch kicked off with FOO_3_4_0001_INITIAL. How can I check?
编辑:我很晚才进入这个项目,并参与了 CVS 到 GIT 的转换。我假设 FOO_3_4_0001... 标记沿以 FOO_3_4_0001_INITIAL 开始的分支的所有标记点。我该如何检查?
EDIT 2: Thanks to everyone who contributed answers. It turns out that tag FOO_3_4_0001 was not on the 3.4 branch and that's what I was missing all along. I have already arranged the lynching party.
编辑 2:感谢所有提供答案的人。事实证明标签 FOO_3_4_0001 不在 3.4 分支上,而这正是我一直缺少的。我已经安排好了私刑派对。
@ghoseb, didn't work. I tried this:
@ghoseb,没用。我试过这个:
16:19:29 (1) foo $ git checkout --track -b FOO_3_4_0001_INITIAL origin/FOO_3_4_0001_INITIAL
Branch FOO_3_4_0001_INITIAL set up to track remote branch refs/remotes/origin/FOO_3_4_0001_INITIAL.
Switched to a new branch "FOO_3_4_0001_INITIAL"
16:19:36 (1) foo $ git branch
* FOO_3_4_0001_INITIAL
master
16:19:39 (1) foo $ git pull
remote: Counting objects: 68, done.
remote: Compressing objects: 100remote: % (59/59), done.
remote: Total 61 (delta 18), reused 0 (delta 0)
Unpacking objects: 100% (61/61), done.
From ssh://***
f0c5a5f..99f6c1e master -> origin/master
Already up-to-date.
16:19:42 (1) foo $
At this point I checked for the change I made on this branch and it's not there. If I check out the code tagged FOO_3_4_0001_RC5a my change is there.
在这一点上,我检查了我在这个分支上所做的更改,但它不存在。如果我查看标记为 FOO_3_4_0001_RC5a 的代码,我的更改就在那里。
Ok, more info for @Dustin and @mipadi.
好的,@Dustin 和 @mipadi 的更多信息。
When I run git branch -r
, I see:
当我运行时git branch -r
,我看到:
16:12:02 (1) foo $ git branch -r
origin/FOO_3_3_0001_BUILT_VF_BRANCH
origin/FOO_3_4_0001_INITIAL
origin/FOO_3_5_0000_RC5
origin/FOO_3_5_0001_BRANCH
origin/HEAD
origin/master
origin/origin
16:12:05 (1) foo $
And if I look at the list of tags, I see, among other things:
如果我查看标签列表,除其他外,我会看到:
16:12:05 (1) foo $ git tag -l
FOO_3_4_0000_RC1
FOO_3_4_0000_RC2
FOO_3_4_0000_RC2b
FOO_3_4_0000_RC3
FOO_3_4_0000_RC4
FOO_3_4_0000_RC4b
FOO_3_4_0000_RC5
FOO_3_4_0000_RELEASE
FOO_3_4_0000_TC1
FOO_3_4_0000_TC2
FOO_3_4_0001_RC2
FOO_3_4_0001_RC3
FOO_3_4_0001_RC4
FOO_3_4_0001_RC5
FOO_3_4_0001_RC5a
FOO_3_4_0001_TC1
16:14:33 (1) foo $
The challenge I have is knowing what the latest code on the FOO_3_4_0001_INITIAL branch is.
我面临的挑战是知道 FOO_3_4_0001_INITIAL 分支上的最新代码是什么。
Now in this instance I happen to know that it's FOO_3_4_0001_RC5a, but if I don't (as will sometimes be the case for some of our developers) how do I check out the origin_FOO_3_4_0001_INITIAL branch know that I've got the very latest code on that branch?
现在在这个例子中,我碰巧知道它是 FOO_3_4_0001_RC5a,但如果我不知道(有时我们的一些开发人员就是这种情况)我如何检查 origin_FOO_3_4_0001_INITIAL 分支知道我有最新的代码那个分支?
采纳答案by mipadi
It soundslike you're doing the right thing. The name of a branch is simply a pointer to the latest commit on the branch. Are you sure you're comming andpushing the changes back to the repo that everyone is pulling from? When you commit, you're only committing to yourlocal repo, so to get changes, collaborations must either (a) pull from your local repo, or (b) pull from some other accessible repo to which you have pushed your changes using git push
.
这听起来像你正在做正确的事情。分支的名称只是指向分支上最新提交的指针。您确定要来并将更改推送回每个人都从中提取的存储库吗?当您提交时,您只提交到您的本地存储库,因此要获得更改,协作必须(a)从您的本地存储库中提取,或(b)从您使用git push
.
回答by Baishampayan Ghose
They can just pull from the remote repository to get the HEAD of the remote branch.
他们只需从远程存储库中拉取即可获取远程分支的 HEAD。
$ git remote update
$ git checkout <branch_name>
$ git pull origin <branch_name>
$ <edit> ...
$ git commit
$ git push origin <branch_name>
I have assumed the name of the remote repository as origin here, you can substitute it with the actual name.
我在此处假设远程存储库的名称为来源,您可以将其替换为实际名称。
回答by andersjanmyr
Git clone, pull and fetch are working with the entire repository tree. The branches are just tags inside your local repository.
Git clone、pull 和 fetch 都在处理整个存储库树。分支只是本地存储库中的标签。
When you have done a clone you have a copy of the remote repository in the origin. You can access this by checking it out locally.
完成克隆后,您在源中拥有远程存储库的副本。您可以通过在本地签出来访问它。
git co -b origin_local_master origin
This creates a local editable branch of the origin master.
这将创建原始主节点的本地可编辑分支。
But you can also access the branches inside this repository.
但是您也可以访问此存储库中的分支。
git co -b origin_local_dev origin/dev
This creates a local editable branch of the origin branch dev.
这将创建原始分支 dev 的本地可编辑分支。
回答by Alexander Mills
If you are talking about a local branch instead of remote branch, both of these work to give you the latest commit hash:
如果您谈论的是本地分支而不是远程分支,那么这两种方法都可以为您提供最新的提交哈希:
git rev-parse HEAD
and
和
git log -n 1 --pretty=format:"%H"
try them both, they should be equivalent
两个都试试,应该是等价的
回答by Paul
Just to make sure we're not overlooking the obvious:
只是为了确保我们没有忽视显而易见的:
When you make changes, do you then commit them before pushing them back? Git branches point to the latest commiton the branch, so if the changes haven't been commited, then the branch will still point to the branch point, which is what you said is happening.
当您进行更改时,您是否在将它们推回之前提交它们?Git 分支指向分支上的最新提交,因此如果更改尚未提交,那么该分支仍将指向分支点,这就是您所说的正在发生的事情。