Skip to content

Checking out a branch

Git Documentation

Switch from the default branch of a repository (working directory) to a new or existing one, syncing any file changes. All future actions are associated with this branch.

Context-aware checking out

During a checkout, gitz inspects the repository for the existence of a branch and intelligently switches between creating a new one or checking out the existing reference.

package main

import (
    "fmt"
    "log"

    git "github.com/purpleclay/gitz"
)

func main() {
    client, _ := git.NewClient()

    out, err := client.Checkout("a-new-branch")
    if err != nil {
        log.Fatal("failed to checkout branch")
    }

    fmt.Println(out)
}

If you were to print the output from the command, you would see a branch creation:

Switched to a new branch 'a-new-branch'

If you check out a branch that already exists, you will see a different output:

Switched to branch 'existing-branch'
Your branch is up to date with 'origin/existing-branch'.