Checkout behavior flags
In libgit2, checkout is used to update the working directory and index
to match a target tree. Unlike git checkout, it does not move the HEAD
commit for you - use git_repository_set_head
or the like to do that.
Checkout looks at (up to) four things: the "target" tree you want to check out, the "baseline" tree of what was checked out previously, the working directory for actual files, and the index for staged changes.
You give checkout one of four strategies for update:
GIT_CHECKOUT_NONE
is a dry-run strategy that checks for conflicts,
etc., but doesn't make any actual changes.GIT_CHECKOUT_FORCE
is at the opposite extreme, taking any action to
make the working directory match the target (including potentially
discarding modified files).In between those are GIT_CHECKOUT_SAFE
and GIT_CHECKOUT_SAFE_CREATE
both of which only make modifications that will not lose changes.
| target == baseline | target != baseline | ---------------------|-----------------------|----------------------| workdir == baseline | no action | create, update, or | | | delete file | ---------------------|-----------------------|----------------------| workdir exists and | no action | conflict (notify | is != baseline | notify dirty MODIFIED | and cancel checkout) | ---------------------|-----------------------|----------------------| workdir missing, | create if SAFE_CREATE | create file |
baseline present | notify dirty DELETED |
---|
The only difference between SAFE and SAFE_CREATE is that SAFE_CREATE will cause a file to be checked out if it is missing from the working directory even if it is not modified between the target and baseline.
To emulate git checkout
, use GIT_CHECKOUT_SAFE
with a checkout
notification callback (see below) that displays information about dirty
files. The default behavior will cancel checkout on conflicts.
To emulate git checkout-index
, use GIT_CHECKOUT_SAFE_CREATE
with a
notification callback that cancels the operation if a dirty-but-existing
file is found in the working directory. This core git command isn't
quite "force" but is sensitive about some types of changes.
To emulate git checkout -f
, use GIT_CHECKOUT_FORCE
.
To emulate git clone
use GIT_CHECKOUT_SAFE_CREATE
in the options.
There are some additional flags to modified the behavior of checkout:
Allow safe updates that cannot overwrite uncommitted data
Allow safe updates plus creation of missing files
Allow all updates to force working directory to look like index
Allow checkout to make safe updates even if conflicts are found
Remove untracked files not in index (that are not ignored)
Remove ignored files not in index
Only update existing files, don't create new ones
Normally checkout updates index entries as it goes; this stops that
Don't refresh index/config/etc before doing checkout
Treat pathspec as simple list of exact match file paths
Allow checkout to skip unmerged files (NOT IMPLEMENTED)
For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED)
For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED)
Recursively checkout submodules with same options (NOT IMPLEMENTED)
Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)