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!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- Python: Merge two dictionaries into one dictionary - Python
- How to undo last Git Commit on Local Repository? - Git
- Execute .bin and .run file Ubuntu Linux - Linux
- SwissCovid App Launched in Switzerland - News
- Not receiving email notification alert in SharePoint Online workflow - Power Automate, FLOW - SharePoint
- List of jar files for Jax-ws (SOAP) based Java Web Services - Java
- How to Print to stderr in Python - Python
- Using Java 8 Predicate with 2 Arguments Example - Java