githug (version 0.0.0.9000)

githug-switch: Switch to another branch

Description

Switch to or "checkout" another branch. Optionally, create the branch if it doesn't exist yet and then check it out.

Usage

git_switch(name = character(), create = NA, repo = ".", rev = "HEAD")
git_branch_checkout(name = "master", create = FALSE, force = FALSE, repo = ".", rev = "HEAD")

Arguments

name
Name of the branch
create
Whether to create the branch if it does not exist yet
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.
force
Whether to overwrite current files with the versions in branch name, even if this means discarding uncommited changes. Default is FALSE.

git_switch

Designed for interactive use. Request a branch by name or, by default, switch to master. If the branch doesn't exist yet, you'll get an offer to create it and immediately check it out. git_switch() will not let you switch branches if you have uncommitted changes that would be lost. You should either:
  1. Commit or stash these changes. Then call git_switch() again.
  2. Use git_branch_checkout() directly, with force = TRUE, if you are willing to lose these changes.

git_branch_checkout

Designed for non-interactive use. If the branch doesn't exist yet, you'll have to explicitly authorize its creation via create = TRUE. force = TRUE will checkout branch name even if it means losing uncommitted changes. In the future, githug will make a branch and a commit or a stash behind the scenes here, in case you later have regrets.

Details

If you currently have uncommitted changes, it is possible -- though not inevitable -- to lose those changes when you switch to another branch. git_switch() will never let that happen. You should seriously consider committing or stashing those at-risk changes. However, if you really want to nuke them, call the lower-level function git_branch_checkout(..., force = TRUE).

Convenience wrappers around git2r's checkout,git_branch-method.

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()

## Not run: 
# ## in an interactive session, try this to checkout and create at once
# git_switch("louise")
# ## End(Not run)

git_branch_checkout("louise", create = TRUE)
git_branch_current()

setwd(owd)

Run the code above in your browser using DataCamp Workspace