Git Branches are one of the fundamental features that make Git so useful. Git Branches allow developers to create parallel versions of their codebase, where they can experiment with new features, make changes, and fix bugs without affecting the main codebase.
To lists all the branches in the current Git repository (local braches and remote branches), you can use the git branch
command
git branch
To switch to the specified branch, you can use the git branch
command followed by the branch name.
git branch <branch-name>
To create a new branch with the specified name and switch to it, you can use the git branch
command followed by -b
flag and the branch name.
git branch -b <branch-name>
To create a new local branch and push it to remote repository, you can use the git branch
command and git push
command.
git checkout -b <branch-name>
git remote add <remote-name> <remote-url>
git push <remote-name> -u <branch-name>
To delete a branch, you can use the git branch
command followed by -d
flag and the branch name.
git branch -d <branch-name>
To delete a remote branch on remote repository, you can use the git branch
command and git push
command.
git checkout -d <branch-name>
git remote add <remote-name> <remote-url>
git push <remote-name> -u <branch-name>
To rename the local branch from the old name to the new name, you can use the git branch
command followed by -m
flag, the current branch name and the new branch name.
git branch -m <old-branch-name> <new-branch-name>
To rename the upstream branch from the old name to the new name, you can use the git push
command.
git remote add <remote-name> <remote-url>
git push <remote-name> -d <old-branch-name>
git push <remote-name> -u <new-branch-name>
To list all local and remote branches from the master in the current Git repository, you can use the git branch
command followed by -a
flag.
git branch -a
To list all remote branches in the current Git repository, you can use the git branch
command followed by -r
flag.
git branch -r
To list all branches with the last commit message in the current directory, you can use the git branch
command followed by -v
flag.
git branch -v
To list all branches that have been merged into the main
branch, you can use the git branch
command followed by --merged
flag.
git branch --merged
To list all branches that have not been merged into the default branches, you can use the git branch
command followed by --no-merged
flag.
git branch --no-merged
To create a new local branch that matches name for a remote tracking branch, you can use the git branch
command followed by -t
flag, remote name and remote branch name.
git remote add <remote-name> <remote-url>
git branch -t <remote-name>/<remote-branch-name>