0

If you have an Ansible playbook and you want to create a task that checks out a specific branch, this stackoverflow question says to do this:

- git:
    repo: git://foosball.example.org/path/to/repo.git
    dest: /srv/checkout
    version: dev

Although version can be the literal string "HEAD", a branch name, a tag, or a 40-character SHA-1 hash, this example assumes that you want to check out the HEAD branch. But what do you do if you have two branches, say a "dev" branch which is a child of a "master" branch, and you want to check out an older commit from the dev branch using a SHA-1 hash? How would you do that? If dev has not yet been merged into master, you would need to specify both the branch name "dev" and the hash. But Ansible doesn't seem to have a separate parameter to allow you do to this. There is a refspec parameter but I'm not sure you can specify a hash in it. In this situation, would you just have to do a fetch and checkout using the Ansible command task and specify the hash in the checkout task?

Jim
  • 13,430
  • 26
  • 104
  • 155
  • Why not just `version: SHA-1`? – phd Apr 06 '20 at 17:41
  • Well, that's what I'm wondering. How does Ansible know which branch to fetch for a given SHA-1, master or dev? And if you have master <- dev <- new_feature and a given hash is in both dev and new_feature, shouldn't you specify a branch? Sorry if these sound like dumb questions but I'm new to Git. – Jim Apr 06 '20 at 17:44
  • "*How does Ansible know which branch to fetch…?*" It doesn't. It just passes the job to `git`; `git` also doesn't know — it fetches enough commits up to the given SHA-1. Branches are not interesting as branches are just labels for commits; commits and chains are interesting but having a SHA-1 reference `git` will fetch all necessary objects. – phd Apr 06 '20 at 17:52
  • "*If…a given hash is in both dev and new_feature, shouldn't you specify a branch?*" Not necessary. `git checkout SHA-1` leads to ["detached head"](https://stackoverflow.com/q/2304087/7976758), with or without ansible. – phd Apr 06 '20 at 17:53

0 Answers0