Search code examples
gitversion-controlgit-branchphing

How to take local working directory to any git revision irrespective of its branch after remote changes?


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.

Solution

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