0

Background

I am working on a phing build script, which takes input as the target revision to which the production codebase needs to be taken. I am preparing the same in a separate scratchpad directory and then overwriting the production codebase.

Current logic

  • During every build, I am simply emptying the scratchpad and taking a fresh clone of the entire git repository in it.
  • Taking to the desired revision -

    git reset --hard ${target.git_version}

I am sure something more efficient can be done. I was thinking along the lines of -

  • finding out which one contains the desired commit, as given in https://stackoverflow.com/a/1419637/351903 (tried but could not get it working with git branch -r --contains <commit> - looks like I am missing something about the concept of it).

  • once the branch is found, cloning that particular branch only.

Then I thought of -

  • getting all the branch names only, into my local repository first (if that is possible and makes sense).

  • then git branch --contains <commit>.

Also thought of -

  • looping through all the branch names and checking if it contains a commit.
Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • Is there a reason you don't simply checkout the commit you want to have with `git checkout ${target.git_version}`? And why wiping and re-cloning always, just a fetch and then the checkout should be enough, shouldn't it? – Vampire May 06 '16 at 14:25
  • @BjörnKautler oh I only knew about `git checkout ` so far. I will try it. Thanks. Just curious, what happens if I try to create a branch name with an existing commit id and then run the command. Does it get the branch or that commit? – Sandeepan Nath May 06 '16 at 14:36
  • 1
    It will warn you that the argument is ambiguous but will prefer using it as branch name as that is most often what you want to checkout – Vampire May 06 '16 at 14:39
  • @BjörnKautler When I started working, I followed the wipe and re-clone approach. Then I started to look for better options. A fetch --all does not seem to solve the purpose because it does not create local branches according to new remote branches. – Sandeepan Nath May 06 '16 at 14:40
  • As well as a clone does not unless you do a mirror clone and if you did a mirror clone also a fetch should add the new branches I think – Vampire May 06 '16 at 14:42
  • Thanks, I will try mirror clone. – Sandeepan Nath May 06 '16 at 15:14

2 Answers2

1

Just checkout the commit you want to have with git checkout ${target.git_version}.
And why wiping and re-cloning always, just a fetch and then the checkout should be enough.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • oh you changed your name it seems.. I found that git clone and fetch do not retrieve information about all the branches existing in the remote repository unless a mirror clone is done. Does your solution suggest using a mirror cloning prior to doing the fetch? – Sandeepan Nath May 10 '16 at 12:35
  • Yeah, back to what it was. I changed it accidentally and you can only change the display name every 30 days. You are wrong. Clone and fetch always get all from the remote repository unless you tell them otherwise by configuration or parameters. You just don't get local branches, but remote branches. (Despite the name remote branches are available locally, they are just in the `refs/remotes/` namespace and represent a branch in the remote repository. You don't need to have a local branch to checkout the revision or even the remote branch. – Vampire May 10 '16 at 12:45
  • Ok. In that case, shouldn't `git branch -v` after a `git fetch --all` show me all the new branches created in the remote repository? This part has been confusing me since quite some time. I am expecting my local repository to have information about the new branches after a fetch. – Sandeepan Nath May 10 '16 at 12:53
  • 1
    `git branch -a` to show all, `git branch -r` to show only remote branches. `-v` and `-vv` only shows more information to the branches shown anyway. – Vampire May 10 '16 at 13:20
0

My script 'Phingit' my help you doing what you want.

Pol Dellaiera
  • 127
  • 1
  • 8