Skip to content

Commit 8de22d5

Browse files
committed
Start of checkout snippets
1 parent c762299 commit 8de22d5

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

docs/guides/101-samples/index.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,3 +1224,66 @@ git_revwalk_simplify_first_parent(walker);
12241224
[`git_revwalk_sorting`](http://libgit2.github.com/libgit2/#HEAD/group/revwalk/git_revwalk_sorting),
12251225
[`git_revwalk_simplify_first_parent`](http://libgit2.github.com/libgit2/#HEAD/group/revwalk/git_revwalk_simplify_first_parent)
12261226
)
1227+
1228+
1229+
<h2 id="checkout">Checkout</h2>
1230+
1231+
<h3 id="checkout_strategies">Strategies</h3>
1232+
1233+
`git_checkout_options` isn't actually very optional.
1234+
The defaults won't be useful outside of a small number of cases.
1235+
The best example of this is `checkout_strategy`; the default value does nothing to the work tree.
1236+
So if you want your checkout to check files out, choose an appropriate strategy.
1237+
1238+
```c
1239+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
1240+
1241+
/* This is kind of like the command-line default */
1242+
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
1243+
/* This is kind of like the -f flag */
1244+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1245+
```
1246+
1247+
(
1248+
[`git_checkout_opts`](http://libgit2.github.com/libgit2/#HEAD/type/git_checkout_opts),
1249+
[checkout header](https://github.com/libgit2/libgit2/blob/HEAD/include/git2/checkout.h#files)
1250+
)
1251+
1252+
<h3 id="checkout_simple">Simple</h3>
1253+
1254+
```c
1255+
/* Checkout from HEAD, something like `git checkout HEAD` */
1256+
int error = git_checkout_head(repo, &opts);
1257+
1258+
/* Checkout from the index */
1259+
error = git_checkout_index(repo, &opts);
1260+
1261+
/* Checkout a different tree */
1262+
git_object *treeish = NULL;
1263+
error = git_revparse_single(&treeish, repo, "feature_branch1");
1264+
error = git_checkout_tree(repo, treeish, &opts);
1265+
```
1266+
1267+
(
1268+
[`git_checkout_head`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_head),
1269+
[`git_checkout_index`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_index),
1270+
[`git_checkout_tree`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_tree),
1271+
[`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single)
1272+
)
1273+
1274+
<h3 id="checkout_paths">Paths</h3>
1275+
1276+
This limits the checkout operation to only certain paths, kind of like `git checkout … -- path/to/a path/to/b`.
1277+
1278+
```c
1279+
char *paths[] = { "path/to/a.txt", "path/to/b.txt" };
1280+
opts.paths.strings = paths;
1281+
opts.paths.count = 2;
1282+
int error = git_checkout_head(repo, &opts);
1283+
```
1284+
1285+
([`git_strarray`](http://libgit2.github.com/libgit2/#HEAD/type/git_strarray))
1286+
1287+
<h3 id="checkout_progress">Progress</h3>
1288+
1289+
<h3 id="checkout_notify">Notify</h3>

0 commit comments

Comments
 (0)