Git - Branches and Tags

Howto work with (topic) branches and tags.


List local branches:

git branch [-v]

List all branches:

git branch -a

List remote branches:

git branch -r

Create a new branch:

git branch mybranchname [reference]

This will create “mybranchname”. If you give a reference e.g. “master”, then the new branch will reference the given branch. Otherwise the reference will be the HEAD of the current branch.

Rename a branch:

git branch -m new-branch
git branch -m new-branch old-branch

Delete a local branch:

git branch -d mybranchname

Delete a remote branch:

git push --delete origin mybranchname

Change your branch: checkout

git checkout mybranchname

Create a new branch and switch to the new branch:

git checkout -b mybranchname


List available tags:

git tag

List tags in alphabetical order:

git tag -l v0.1.*

Create annotated tag (stored as full objects in git):

# create the tag
git tag -a v0.0.1 -m '1st working version'
# show tag
git show v0.0.1
# list tags with tag message (if present)
git tag -l -n1

Create lightweigth tag (pointer to a specific commit):

git tag v.0.0.2

Upload tag to remote

# tags are not automatically uploaded to origin, you must do it on purpose:
git push origin v1.0.0
# or push ALL tags - use with caution!
git push origin --tags


Before merging, you'd want to verify what is different to the current (e.g. master) branch:

git diff master..branch
git log master..branch
git shortlog master..branch
git diff --name-status master..branch
git diff --name-only master..branch

Howto merge branches

# integrate the given branch name into the checked out (working) branch
git merge otherbranch
# merge fast-forward:
git merge --ff-only otherbranch
# merge with merge commit:
git merge --no-ff otherbranch
# show which branches have already merged into master:
git branch --merged master
# the shown branches may be safly deleted


rebase your topic branch if it is behind your master branch:

# rebase your current checked out branch:
git rebase master
# rebase a branch which is not checkout out:
git rebase master topic2

Don't rebase commits your already published!


show a tree of branches:

git log --decorate --pretty=oneline --abbrev-commit --graph --all
howto/git/branches_and_tags.txt · Last modified: 2017-11 by tb
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready