git2r (version 0.10.1)

checkout: Checkout

Description

Update files in the index and working tree to match the content of the tree pointed at by the treeish object (commit, tag or tree). Checkout using the default GIT_CHECKOUT_SAFE_CREATE strategy (force = FALSE) or GIT_CHECKOUT_FORCE (force = TRUE).

Usage

checkout(object, ...)

## S3 method for class 'git_repository': checkout(object, branch, create = FALSE, force = FALSE)

## S3 method for class 'git_branch': checkout(object, force = FALSE)

## S3 method for class 'git_commit': checkout(object, force = FALSE)

## S3 method for class 'git_tag': checkout(object, force = FALSE)

Arguments

object
A repository, commit, tag or tree which content will be used to update the working directory.
...
Additional arguments affecting the checkout
branch
If object is a repository, the name of the branch to check out.
create
If object is a repository, then branch is created if doesn't exist.
force
If TRUE, then make working directory match target. This will throw away local changes. Default is FALSE.

Value

  • invisible NULL

Examples

Run this code
## Create directories and initialize repositories
path_bare <- tempfile(pattern="git2r-")
path_repo_1 <- tempfile(pattern="git2r-")
path_repo_2 <- tempfile(pattern="git2r-")
dir.create(path_bare)
dir.create(path_repo_1)
dir.create(path_repo_2)
repo_bare <- init(path_bare, bare = TRUE)

## Clone to repo 1 and config user
repo_1 <- clone(path_bare, path_repo_1)
config(repo_1, user.name="Alice", user.email="alice@example.org")

## Add changes to repo 1 and push to bare
writeLines("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
           con = file.path(path_repo_1, "test.txt"))
add(repo_1, "test.txt")
commit(repo_1, "First commit message")
push(repo_1, "origin", "refs/heads/master")

## Create and checkout 'dev' branch in repo 1
checkout(repo_1, "dev", create = TRUE)

## Add changes to 'dev' branch in repo 1 and push to bare
writeLines(c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
             "eiusmod tempor incididunt ut labore et dolore magna aliqua."),
           con = file.path(path_repo_1, "test.txt"))
add(repo_1, "test.txt")
commit(repo_1, "Second commit message")
push(repo_1, "origin", "refs/heads/dev")

## Clone to repo 2
repo_2 <- clone(path_bare, path_repo_2)
config(repo_2, user.name="Bob", user.email="bob@example.org")

## Read content of 'test.txt'
readLines(file.path(path_repo_2, "test.txt"))

## Checkout dev branch
checkout(repo_2, "dev")

## Read content of 'test.txt'
readLines(file.path(path_repo_2, "test.txt"))

Run the code above in your browser using DataLab