Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Fetching latest commit on a branch

Tags:

git

branch

I'm struggling to understand something about 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.

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?

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.


@ghoseb, didn't work. I tried this:

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.


Ok, more info for @Dustin and @mipadi.

When I run git branch -r, I see:

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.

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?

like image 936
Andrew Avatar asked Jan 19 '09 04:01

Andrew


People also ask

How do I get the latest commit of a branch?

If you need to get latest commit on a branch through the REST api, try the "/commits" call with the "until" parameter set to the branch you are interested in, and limit=1 to get the single tip commit from the branch.

How do you pull the last commit from a git branch?

This can be done using git reset . First, make sure you have the most recent copy of that remote tracking branch by fetching. Then, use git reset --hard to move the HEAD pointer and the current branch pointer to the most recent commit as it exists on that remote tracking branch.

Does git fetch update current branch?

You can do a git fetch at any time to update your remote-tracking branches under refs/remotes/<remote>/ . This command does not change any of your own local branches under refs/heads , and is safe to do without changing your working copy.


2 Answers

They can just pull from the remote repository to get the HEAD of the remote branch.

$ 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.

like image 164
Baishampayan Ghose Avatar answered Sep 27 '22 21:09

Baishampayan Ghose


It sounds like 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 and pushing the changes back to the repo that everyone is pulling from? When you commit, you're only committing to your local 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.

like image 29
mipadi Avatar answered Sep 27 '22 21:09

mipadi