Skip to content

Branches

Multiple branches can be made to focus changes on specific efforts. These are cut from the current branch; most cases this should be master.

Create a Branch

# List, create, and move to new branch.
git branch -a
git checkout -b {NEW BRANCH}
Use git normally.

Merging Branches

Completed branches can be merged back into any branch, typically master.

# List branches, switch to master, and merge.
git branch -a
git checkout master
git merge {BRANCH} --no-ff

Note

--no-ff retains all commit messages from the branch. Leave this off to squish the commit (it may be helpful to get branch log for merge message git --no-pager log > /tmp/git.log).

You may reset the merge before committing with no data loss with *get merge --abort.

Deleting Branches

Tip

Git will throw an error if deleting a branch with commits that has not been merged.

# Delete merged branch.
git branch -a
git branch -d {BRANCH}

Create Worktree

Allows the use of multiple branches simultaneously.

# Create a new worktree from a branch
git branch -a
git checkout -b {NEW BRANCH}
git checkout master
git worktree add ../{WORKTREE NAME} {BRANCH}

Merge Worktree

Works like normal branch merging. Execute merge from master worktree.

Removing Worktree

After the branch is merged worktree can be removed.

# Remove worktree.
cd {MASTER WORKTREE}
git worktree remove {WORKTREE}

Then delete branch as normal. See Deleting Branches.