Use the git resource to manage source control resources that exist in a git repository. git version 1.6.5 (or higher) is required to use all of the functionality in the git resource.


This resource is often used in conjunction with the deploy resource.


A git resource block manages source control resources that exist in a git repository:

git "#{Chef::Config[:file_cache_path]}/app_name" do
  repository node[:app_name][:git_repository]
  revision node[:app_name][:git_revision]
  action :sync

The full syntax for all of the properties that are available to the git resource is:

git 'name' do
  additional_remotes         Hash
  checkout_branch            String
  depth                      Integer
  destination                String # defaults to 'name' if not specified
  enable_checkout            TrueClass, FalseClass
  enable_submodules          TrueClass, FalseClass
  environment                Hash
  group                      String, Integer
  notifies                   # see description
  provider                   Chef::Provider::Scm::Git
  reference                  String
  remote                     String
  repository                 String
  revision                   String
  ssh_wrapper                String
  subscribes                 # see description
  timeout                    Integer
  user                       String, Integer
  action                     Symbol # defaults to :sync if not specified


  • git is the resource
  • name is the name of the resource block and also (when the destination property is not specified) the location in which the source files will be placed and/or synchronized with the files under source control management
  • :action identifies the steps the chef-client will take to bring the node into the desired state
  • additional_remotes, checkout_branch, depth, destination, enable_checkout, enable_submodules, environment, group, provider, reference, remote, repository, revision, ssh_wrapper, timeout, and user are properties of this resource, with the Ruby type shown. See “Properties” section below for more information about all of the properties that may be used with this resource.


This resource has the following actions:

Action Description
:checkout Clone or check out the source. When a checkout is available, this provider does nothing.
:export Export the source, excluding or removing any version control artifacts.
:nothing Define a resource block that does nothing until it is notified by another resource to take action. When notified, this resource block is either run immediately or it is queued up to be run at the end of the chef-client run.
:sync Default. Update the source to the specified version, or get a new clone or checkout. This action causes a hard reset of the index and working tree, discarding any uncommitted changes.


This resource has the following properties:

Property Description

Ruby Type: Hash

An array of additional remotes that are added to the git repository configuration.


Ruby Type: String

Do a one-time checkout from git or use when a branch in the upstream repository is named deploy. To prevent the git resource from attempting to check out master from master, set enable_checkout to false when using the checkout_branch property. See revision. Default value: deploy.


Ruby Type: Integer

The number of past revisions to be included in the git shallow clone. The default behavior will do a full clone.


Ruby Type: String

The location path to which the source is to be cloned, checked out, or exported. Default value: the name of the resource block. See “Syntax” section above for more information.


Ruby Types: TrueClass, FalseClass

Check out a repo from master. Set to false when using the checkout_branch attribute to prevent the git resource from attempting to check out master from master. Default value: true.


Ruby Types: TrueClass, FalseClass

Perform a sub-module initialization and update. Default value: false.


Ruby Type: Hash

A Hash of environment variables in the form of {"ENV_VARIABLE" => "VALUE"}. (These variables must exist for a command to be run successfully.)


The git provider automatically sets the ENV['HOME'] and ENV['GIT_SSH'] environment variables. To override this behavior and provide different values, add ENV['HOME'] and/or ENV['GIT_SSH'] to the environment Hash.


Ruby Types: String, Integer

The system group that is responsible for the checked-out code.


Ruby Types: TrueClass, FalseClass

Continue running a recipe if a resource fails for any reason. Default value: false.


Ruby Type: Symbol, ‘Chef::Resource[String]’, Symbol

Which resource takes action when this resource’s state changes. A resource may notify more than one resource; use a notifies statement for each resource to be notified.

Specify the :action, 'resource[name]', and timer (:delayed or :immediately). Use multiple notifies statements to notify more than one resource.

resource 'name' do
  notifies :action, 'resource[name]', :timer

Use the following timers to specify when a notification is triggered:

Timer Description
:delayed Use to specify that a notification should be queued up, and then executed at the very end of a chef-client run.
:immediately Use to specify that a notification should be run immediately, per resource notified.

Ruby Type: Chef Class

Optional. Explicitly specify a provider.


Ruby Type: String

The alias for revision.


Ruby Type: String

The remote repository to use when synchronizing an existing clone.


Ruby Type: String

The URI for the git repository.


Ruby Type: Integer

The number of times to catch exceptions and retry the resource. Default value: 0.


Ruby Type: Integer

The retry delay (in seconds). Default value: 2.


Ruby Type: String

A branch, tag, or commit to be synchronized with git. This can be symbolic, like HEAD or it can be a source control management-specific revision identifier. See checkout_branch. Default value: HEAD.

The value of the revision attribute may change over time. From one branch to another, to a tag, to a specific SHA for a commit, and then back to a branch. The revision attribute may even be changed in a way where history gets rewritten.

Instead of tracking a specific branch or doing a headless checkout, the chef-client maintains its own branch (via the git resource) that does not exist in the upstream repository. The chef-client is then free to forcibly check out this branch to any commit without destroying the local history of an existing branch.

For example, to explicitly track an upstream repository’s master branch:

revision 'master'

Use the git rev-parse and git ls-remote commands to verify that the chef-client is synchronizing commits correctly. (The chef-client always runs git ls-remote on the upstream repository to verify the commit is made to the correct repository.)


Ruby Type: String

The path to the wrapper script used when running SSH with git. The GIT_SSH environment variable is set to this.


Ruby Type: Symbol, ‘Chef::Resource[String]’, Symbol

Specify that this resource is to listen to another resource, and then take action when that resource’s state changes.

Specify the :action, 'resource[name]', and timer (:delayed or :immediately). Use multiple subscribes statements to listen to more than one resource.

resource 'name' do
  subscribes :action, 'resource[name]', :timer

The subscribes property uses the same timers as the notifies property.


Ruby Type: Integer

The amount of time (in seconds) to wait for a command to execute before timing out. When this property is specified using the deploy resource, the value of the timeout property is passed from the deploy resource to the git resource.


Ruby Types: String, Integer

The system user that is responsible for the checked-out code. Default value: the home directory of this user, as indicated by the HOME environment variable.


The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains:

Use the git mirror

git '/opt/mysources/couch' do
  repository 'git://'
  revision 'master'
  action :sync

Use different branches

To use different branches, depending on the environment of the node:

if node.chef_environment == 'QA'
   branch_name = 'staging'
   branch_name = 'master'

git '/home/user/deployment' do
   repository ''
   revision branch_name
   action :sync
   user 'user'
   group 'test'

where the branch_name variable is set to staging or master, depending on the environment of the node. Once this is determined, the branch_name variable is used to set the revision for the repository. If the git status command is used after running the example above, it will return the branch name as deploy, as this is the default value. Run the chef-client in debug mode to verify that the correct branches are being checked out:

$ sudo chef-client -l debug

Install an application from git using bash

The following example shows how Bash can be used to install a plug-in for rbenv named ruby-build, which is located in git version source control. First, the application is synchronized, and then Bash changes its working directory to the location in which ruby-build is located, and then runs a command.

 git "#{Chef::Config[:file_cache_path]}/ruby-build" do
   repository 'git://'
   reference 'master'
   action :sync

 bash 'install_ruby_build' do
   cwd '#{Chef::Config[:file_cache_path]}/ruby-build'
   user 'rbenv'
   group 'rbenv'
   code <<-EOH
   environment 'PREFIX' => '/usr/local'

To read more about ruby-build, see here:

Upgrade packages from git

The following example uses the git resource to upgrade packages:

# the following code sample comes from the ``source`` recipe
# in the ``libvpx-cookbook`` cookbook:

git "#{Chef::Config[:file_cache_path]}/libvpx" do
  repository node[:libvpx][:git_repository]
  revision node[:libvpx][:git_revision]
  action :sync
  notifies :run, 'bash[compile_libvpx]', :immediately

Pass in environment variables

git '/opt/mysources/couch' do
  repository 'git://'
  revision 'master'
  environment  { 'VAR' => 'whatever' }
  action :sync