githug (version 0.0.0.9000)

git_branch_create: Create a new branch

Description

Create a new local branch. You must specify the name of the new branch, at the very least. By default, the new branch will point at current HEAD. Optionally, you can specify another commit to base the branch on, via a revision string, e.g. HEAD^, branchname, SHA-1 or a leading substring thereof.

Usage

git_branch_create(name, repo = ".", rev = "HEAD")

Arguments

name
Name for the new branch
repo
Path to a Git repo. If unspecified, current working directory is checked to see if it is or is inside a Git repo.
rev
Target commit, as a revision string, e.g. HEAD^, branchname, SHA-1 or a leading substring thereof.

Details

Convenience wrapper around git2r::branch_create().

Examples

Run this code
repo <- git_init(tempfile("githug-branches-"))
owd <- setwd(repo)

## first commit
writeLines("Well, we're not in the middle of nowhere...", "nowhere.txt")
git_commit("nowhere.txt", message = "1: not in the middle of nowhere")
git_branch_list()

## second commit
write("but we can see it from here.", "nowhere.txt", append = TRUE)
git_commit("nowhere.txt", message = "2: but we can see it from here")

## create new branch that points at HEAD = second commit
git_branch_create("carpe_diem")
git_branch_list()

## create new branch that points at *first commit*, via its SHA
(gl <- git_history())
git_branch_create("hindsight", rev = gl$sha[[2]])
git_branch_list()

## Not run: 
# ## try to re-create an existing branch and fail
# git_branch_create("hindsight")
# ## End(Not run)

setwd(owd)

Run the code above in your browser using DataCamp Workspace