Example 1: Create a new branch
Syntax:
This is the most common way a developer creates a new git branch using the console.
Example:
% git branch ticket-2023
In this example a new branch with name ticket-2023 will be created from the current branch (main in this case) and will point to the same commit as the branch that you were on (main in our case) while executing the branch command.

Example 2: Create a new branch and switch to it
You can switch to the new branch using the command git checkout command.
% git checkout ticket-2023
Switched to branch 'ticket-2023'
But if you want to create a new branch as well as switch to it immediately then make use of the -b flag.
Syntax:
Example:
% git -b branch ticket-2023-07
Switched to a new branch 'ticket-2023-07'
As you can see we were able to switch to the newly created branch immediately in one command.
Example 3: Create a new branch from an existing branch
As we saw in Example 1 when we run the git branch command with just the new branch name, the new branch is created from the current branch you are on, but what if you want to create a new branch from a specific branch? In such a case you can pass in the from-branch-name as a parameter.
Syntax:
Example:
% git branch ticket-2023-08 ticket-2023-07
Example 4: Create a new branch from a commit
Like the same way we created a branch from another branch, we can make use of the commit-hash as well to create a new branch.
Syntax:
Example:
% git log --oneline
216e7af (HEAD -> ticket-2023-07) some commit
a348bd9 (ticket-2023-08, ticket-2023, main) init commit
% git branch branch-new-123 a348bd9
Example 5: Create a new branch from the remote origin branch
If you do not have a branch locally or you want to create one from a remote git origin server (bitbucket, Github, Gitlab), you can do that as follows,
Syntax:
Example:
% git branch ticket-2025 origin/master
Example 6: Create a new branch using tags
Syntax:
Example:
% git branch ticket-2026 tags/v.3.5
Facing issues? Have Questions? Post them here! I am happy to answer!
- Fix [oh-my-zsh] Cant update: not a git repository
- [fix] fatal: pathspec index.html did not match any files error
- How to ignore files in git using .gitignore file
- Git Fix: fatal: refusing to merge unrelated histories Error
- Install GitHub Command Line Tool on Mac
- How to change directory in Git bash
- Get List of all local branches git command
- fix fatal: --local can only be used inside a git repository error
- [git] fatal: your current branch 'main' does not have any commits yet
- Git Revision Questions Before the Interview
- Change the default git branch name from master to main
- git command to remove/unstage files from staging area
- Remove git config at Local, Global or System Levels?
- [fix] fatal: this operation must be run in a work tree in git
- Clone a particular remote brach using git clone command
- How do I get a list of all branches in Git?
- How to check your installed version of Git
- Step-by-Step: How to delete a git branch from local as well as remote origin
- Git Commit - Author identity unknown, Please tell me who you are email
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to Rename a Git Local Branch Using the --move Option
- Perform an Empty Commit in Git without anything in Staging Area
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- GitHub: How to Search Code
- Rename git branch on Local and GitHub Remove using Command
- Java monitoring and management console [jconsole] - Java
- JSON column formatting to preview SharePoint Online file on mouse hover - SharePoint
- 3 Examples to read a text file line by line using java - Java
- MySQL: Error Code: 1175 You are using safe update mode - MySQL
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException - Java
- How to hide or cancel Toast message in Android Programming - Android
- [Android] Sharing Multiple Images to Twitter App from your App - Twitter
- How to Stop Mac from Sleeping (macOS Ventura and Above) - MacOS