# Git Branches Management ## Metadata **Status**:: #x **Zettel**:: #zettel/fleeting **Created**:: [[2026-01-05]] **Topic**:: [[♯ Git]] ## Branches Hierarchy Show hierarchy of local branches > [!info] alias `git bg` > ```shell > git log --graph --oneline --simplify-by-decoration --branches --decorate-refs refs/heads/* > ``` Show hierarchy of local branches and whether they are synchronized with remote branches > [!info] alias `git bgm` > ```shell > git log --graph --oneline --simplify-by-decoration --branches --decorate-refs "refs/heads/*" --decorate-refs "refs/remotes/*" "$(git mbase)^.." > ``` Where `git mbase` is the heuristic base branch for pull requests > [!info] alias `git mbase` > ```shell > local mb="$(git config "branch.$(git branch --show-current).vscode-merge-base" || git symbolic-ref refs/remotes/upstream/HEAD 2>/dev/null || git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null)" > echo "${mb#refs/remotes/}" > ``` A python script to show branches as a tree: [git-branch-tree](https://github.com/doitian/dotfiles-public/blob/master/default/bin/git-branch-tree) ## Synchronize Base Branch > [!info] alias `git msync` > ```shell > local mb="$(git mbase)" > git fetch --all --prune > # merge base into local > git fetch . ${mb}:${mb#*/} 2>/dev/null || git merge --ff-only > # synchronize the base branch from upstream to origin (fork) > if [ "${mb%%/*}" = upstream ]; then > git push origin ${mb}:${mb#*/} > fi > ``` ## Rebase From Base Branch ### Use Git ```shell git rebase -i "$(git mbase)" ``` ### Use LazyGit Include `develop` in the `mainBranches` config: ```yaml git: mainBranches: ["develop", "main", "master"] ``` Press <kbd>r</kbd> then <kbd>b</kbd> to rebase onto the base branch. ## Rebase Stacked Branches - `git config --global rebase.updaterefs true` - Rebase from leaf branches, which will update branch refs for all ancestor branches. If there are forks and changes occur in a common ancestor branch, rebase from one leaf branch, then use `git rebase the-fork-point --onto the-fork-point` to rebase other leaves.