git branch, git branch --show-current, git branch -m
git branch [--list] [<pattern>...]
git branch --show-current
git branch <branchname>
git branch -m <oldbranch> <newbranch>List local branches, create a name without switching, and rename a branch.
| Parameter | Description |
|---|---|
<branchname> | The new branch name. The new ref points at HEAD, and the current branch does not change. |
--show-current | Print only the current branch name, or print nothing in detached HEAD. |
-m <old> <new> | Rename a branch. Use this to rename a branch you are not currently on; git switch has its own rename form if you need to rename the branch you are on. |
--merged / --no-merged | List branches that are, or are not, reachable from HEAD (or from a given commit). |
Returns: A branch list on standard output, or a new or renamed ref. The working tree and index are not changed by a plain create or rename of another branch.
- git branch -a and -r list remote-tracking names as well. This example stays with local branches.
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'base\n' > README.md
git add README.md
git commit -m "Add README" >/dev/null
echo "show-current"
git branch --show-current
git branch topic
echo "after-create current=$(git branch --show-current)"
echo "list"
git branch --list
git branch -m topic feature
echo "after-rename"
git branch --list
echo "merged"
git branch --merged
echo "no-merged"
nm=$(git branch --no-merged)
if [ -z "$nm" ]; then echo "(empty-list)"; else printf '%s\n' "$nm"; fi
Expected output
show-current
main
after-create current=main
list
* main
topic
after-rename
feature
* main
merged
feature
* main
no-merged
(empty-list)
Related: git switch, git merge