add: Add file(s) to index

Description Usage Arguments Value Examples

View source: R/index.R

Description

Add file(s) to index

Usage

1
add(repo = ".", path = NULL, force = FALSE)

Arguments

repo

a path to a repository or a git_repository object. Default is '.'

path

Character vector with file names or shell glob patterns that will matched against files in the repository's working directory. Each file that matches will be added to the index (either updating an existing entry or adding a new entry).

force

Add ignored files. Default is FALSE.

Value

invisible(NULL)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Not run: 
## Initialize a repository
path <- tempfile(pattern="git2r-")
dir.create(path)
repo <- init(path)

## Create a user
config(repo, user.name = "Alice", user.email = "alice@example.org")

## Create a file
writeLines("a", file.path(path, "a.txt"))

## Add file to repository and view status
add(repo, "a.txt")
status(repo)

## Add file with a leading './' when the repository working
## directory is the current working directory
setwd(path)
writeLines("b", file.path(path, "b.txt"))
add(repo, "./b.txt")
status(repo)

## Add a file in a sub-folder with sub-folder as the working
## directory. Create a file in the root of the repository
## working directory that will remain untracked.
dir.create(file.path(path, "sub_dir"))
setwd("./sub_dir")
writeLines("c", file.path(path, "c.txt"))
writeLines("c", file.path(path, "sub_dir/c.txt"))
add(repo, "c.txt")
status(repo)

## Add files with glob expansion when the current working
## directory is outside the repository's working directory.
setwd(tempdir())
dir.create(file.path(path, "glob_dir"))
writeLines("d", file.path(path, "glob_dir/d.txt"))
writeLines("e", file.path(path, "glob_dir/e.txt"))
writeLines("f", file.path(path, "glob_dir/f.txt"))
writeLines("g", file.path(path, "glob_dir/g.md"))
add(repo, "glob_dir/*txt")
status(repo)

## Add file with glob expansion with a relative path when
## the current working directory is inside the repository's
## working directory.
setwd(path)
add(repo, "./glob_dir/*md")
status(repo)

## End(Not run)

Example output

Staged changes:
	New:        a.txt

Staged changes:
	New:        a.txt
	New:        b.txt

Untracked files:
	Untracked:  c.txt

Staged changes:
	New:        a.txt
	New:        b.txt
	New:        sub_dir/c.txt

Untracked files:
	Untracked:  c.txt
	Untracked:  glob_dir/g.md

Staged changes:
	New:        a.txt
	New:        b.txt
	New:        glob_dir/d.txt
	New:        glob_dir/e.txt
	New:        glob_dir/f.txt
	New:        sub_dir/c.txt

Untracked files:
	Untracked:  c.txt

Staged changes:
	New:        a.txt
	New:        b.txt
	New:        glob_dir/d.txt
	New:        glob_dir/e.txt
	New:        glob_dir/f.txt
	New:        glob_dir/g.md
	New:        sub_dir/c.txt

git2r documentation built on Nov. 22, 2021, 9:08 a.m.

Related to add in git2r...