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




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


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.

Answered By – Vampire

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More