git branch and worktree Snippets
# Create a new branchgit checkout -b new-branch-name# make changes to new-branch-name# Merge changes to main branchgit checkout maingit merge new-branch-name
# Checkout an existing branchgit checkout my-branch
# List all git branches, local, and remotes in current repositorygit branch -a
# Create new branch and push to origingit checkout -b feature1git push origin feature1
# Check differences between local "main" branch and remotegit diff main remotes/origin/HEAD
# List branchesgit branch -a -v
# Switch to git (remote) branch named "Release_3.0"git switch Release_3.0
# Remove temporary branch named "list"git branch -d list
# Go back to last branchgit checkout -Branching and Merging
Section titled “Branching and Merging”Source: Git - Basic Branching and Merging
# Run visual tool to help with mergesgit mergetoolOptions for Keeping Feature Branches Up-To-Date with Main
Section titled “Options for Keeping Feature Branches Up-To-Date with Main”Source: Best Practices for Keeping Feature Branches Up-To-Date with Main · community GitHub
Generally, use rebase option for small teams and merge option for large teams.
Rebase
Section titled “Rebase”Pros:
- Clean, linear commit history, making it easier to read and review.
- Avoids merge commits, which can clutter the history.
- Preferred for small teams or solo developers who value a tidy Git log.
Cons:
- Complex if multiple developers are working on the same branch, as rebasing rewrites history and requires coordination.
- Conflicts must be resolved all at once during the rebase, which can be time-consuming for long-lived branches.
# Ensure you're on your feature branchgit checkout feature-branch# Fetch the latest main branchgit fetch origin# Rebase feature branch onto maingit rebase origin/main## Identify conflicts during rebasegit status## Make changes and continuegit rebase --contiuePros:
- Safer for collaborative branches, as it doesn’t rewrite history.
- Easier to understand for teams new to Git.
- Preserves the context of when and why main was integrated.
Cons:
- Creates merge commits, which can make the history less clean.
- Can lead to a tangled Git graph if merges are frequent.
# Ensure you're on your feature branchgit checkout feature-branch# Fetch the latest main branchgit fetch origin# Merge main into your branchgit merge origin/main# Push the updated branchgit push origin feature-branch
# Merge conflicts manualygit add <resolved-file>git commitWorktrees - git worktree
Section titled “Worktrees - git worktree”Git worktrees help with manage multiple working trees and it is easier to keep track of separate branches. See Worktrunk Snippets - Worktrunk Snippets for command to simplify management of worktrees.
They are like managing a git branch as a separate directory or manually git cloning a repository to a separate directory and changing the working branch of that repository.
# Create a new branch whose name is the final part of the command; add a new worktree with a feature branchgit worktree add ../myrepo-newfeature new-feature## Example## /bubbles is [master]## /bubbles-table is branch [use-lipgloss-table]
# Create new worktree with existing branch maingit worktree add ../repo-main main
# Example workflow with these branches:# - main# - pull request review# - feature developmentgit clone myrepo.git # maincd myrepogit worktree add ../myrepo-feature feature_branchgit worktree add ../myrepo-pr pull_request_numbered_branch
# If you want to make experimental changes or do testing without disturbing development,# it is often convenient to create a throwaway worktree not associated with any branch.# Creates a new worktree with a detached HEAD at the same commit as the current branch examplegit worktree add -d ../repo-experimental
# List work trees, their location, and checked out branchesgit worktree list
# Remove a worktreegit worktree remove worktree-name
# Merge a worktree changes to main branchgit checkout maingit merge worktree-branchgit worktree remove worktree-branchExample worktree workflow from Stop Using Git Worktrees - DevOps Toolbox, YouTube
# Create a folder to contain worktrees of a repositorygit clone --bare https://my.repository/repo2 repo2## Add main branch as a subfolder in repo2git worktree add main# Create a new branch and push to origingit checkout -b feature1git push origin feature1## Add feature1 branch as a subfolder in repo2git worktree add ../feature1## List worktreesgit worktree list## Do work in feature1 and then merge it to main and remove itcd maingit merge feature1git worktree remove feature1git branch -d feature1