<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shoaib Allam</title>
    <description>The latest articles on DEV Community by Shoaib Allam (@shoaiballam).</description>
    <link>https://dev.to/shoaiballam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4253905%2F20e9e049-6db8-49cf-8361-44f4d308bc4f.png</url>
      <title>DEV Community: Shoaib Allam</title>
      <link>https://dev.to/shoaiballam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shoaiballam"/>
    <language>en</language>
    <item>
      <title>How to Create a New Branch in Git (and Switch to It)</title>
      <dc:creator>Shoaib Allam</dc:creator>
      <pubDate>Thu, 19 Nov 2026 13:18:02 +0000</pubDate>
      <link>https://dev.to/shoaiballam/how-to-create-a-new-branch-in-git-and-switch-to-it-3g57</link>
      <guid>https://dev.to/shoaiballam/how-to-create-a-new-branch-in-git-and-switch-to-it-3g57</guid>
      <description>&lt;p&gt;&lt;strong&gt;To create a new branch in Git and switch to it, run &lt;code&gt;git switch -c &amp;lt;branch-name&amp;gt;&lt;/code&gt;.&lt;/strong&gt; On Git versions older than 2.23, use &lt;code&gt;git checkout -b &amp;lt;branch-name&amp;gt;&lt;/code&gt;, which does exactly the same thing. To create a branch without switching to it, run &lt;code&gt;git branch &amp;lt;branch-name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create and switch: &lt;code&gt;git switch -c feature/login&lt;/code&gt;. Create only: &lt;code&gt;git branch feature/login&lt;/code&gt;. Switch: &lt;code&gt;git switch feature/login&lt;/code&gt;. List all branches: &lt;code&gt;git branch -a&lt;/code&gt;. Put the new branch on GitHub: &lt;code&gt;git push -u origin feature/login&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we tested:&lt;/strong&gt; every command on this page was run in a real Git repository (Git 2.43) on 19 November 2026, and the output shown is copied from the terminal. Your commit hashes and branch names will differ.&lt;/p&gt;

&lt;p&gt;A branch is a separate line of work inside the same project. You make one when you start a feature or a fix, do all your commits there, and merge it back when it's ready. Your main branch stays working the whole time, and if the idea doesn't pan out you delete the branch and nothing else is touched. This guide covers the Git commands to create a new branch (the right way, with real terminal output), switch between branches, list them, base a branch on an older commit or a remote branch, and push it to GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a branch actually is
&lt;/h2&gt;

&lt;p&gt;A branch in Git is a lightweight pointer to a commit, not a copy of your files. That's why creating one is instant, even in a huge project. When you commit on a branch, the pointer moves forward to the new commit, and other branches stay where they were. If you want the bigger picture of commits and repositories first, read &lt;a href="https://gitcommands.si/what-is-git/" rel="noopener noreferrer"&gt;what Git is and how it works&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a new branch in Git and switch to it
&lt;/h2&gt;

&lt;p&gt;This is the command you'll use most:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git switch -c bugfix/header
Switched to a new branch &lt;span class="s1"&gt;'bugfix/header'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-c&lt;/code&gt; stands for "create". Git makes the branch from the commit you're on right now and moves you onto it in one go. Any new commits land on &lt;code&gt;bugfix/header&lt;/code&gt;, and &lt;code&gt;main&lt;/code&gt; stays untouched.&lt;/p&gt;

&lt;h3&gt;
  
  
  The older way: git checkout -b
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git checkout -b feature/profile
Switched to a new branch &lt;span class="s1"&gt;'feature/profile'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same result. &lt;code&gt;git switch&lt;/code&gt; was added in Git 2.23 (2019) because &lt;code&gt;git checkout&lt;/code&gt; does too many unrelated jobs, including restoring files, which makes it easy to type the wrong thing. Both commands work today, and you'll see &lt;code&gt;checkout -b&lt;/code&gt; in a lot of older tutorials, scripts and Git commands cheat sheets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a new branch without switching to it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git branch feature/search
$ git branch
  feature/login
  feature/search
* main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;git branch &amp;lt;name&amp;gt;&lt;/code&gt; creates the branch and prints nothing. You stay where you were; the star in the list shows your current branch is still &lt;code&gt;main&lt;/code&gt;. This is handy when you want to bookmark the current commit before trying something risky.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch between branches
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git switch feature/search
Switched to branch &lt;span class="s1"&gt;'feature/search'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git swaps the files in your folder to match that branch. Two shortcuts worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git switch -&lt;/code&gt; jumps back to the branch you were on before, like the back button in a browser.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git branch --show-current&lt;/code&gt; prints just the name of the branch you're on, which helps in scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When Git won't let you switch
&lt;/h3&gt;

&lt;p&gt;Uncommitted edits normally travel with you to the new branch. But if the other branch changed the same file, Git stops to protect your work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git switch feature/search
error: Your &lt;span class="kd"&gt;local&lt;/span&gt; changes to the following files would be overwritten by checkout:
    app.js
Please commit your changes or stash them before you switch branches.
Aborting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing is lost. Either commit your changes first, or park them with &lt;code&gt;git stash&lt;/code&gt;, switch, and bring them back later with &lt;code&gt;git stash pop&lt;/code&gt;. Our &lt;a href="https://gitcommands.si/git-stash/" rel="noopener noreferrer"&gt;git stash guide&lt;/a&gt; explains how stashing works.&lt;/p&gt;

&lt;h2&gt;
  
  
  List branches: local, remote and all
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want to see…&lt;/th&gt;
&lt;th&gt;Git command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Local branches&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remote branches only&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch -r&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All branches, local and remote&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch -a&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Branches with their latest commit&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch -v&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Which remote branch each one tracks&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch -vv&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Only branches matching a pattern&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git branch --list 'feature/*'&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here's what &lt;code&gt;git branch -a&lt;/code&gt; looks like in practice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git branch -a
  bugfix/header
  feature/login
* feature/profile
  feature/search
  main
  remotes/origin/feature/login
  remotes/origin/main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The lines starting with &lt;code&gt;remotes/origin/&lt;/code&gt; are Git's record of the branches on GitHub (or whatever your remote is) as of your last fetch. If a teammate just pushed a branch and you can't see it, run &lt;code&gt;git fetch&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;git branch -vv&lt;/code&gt; tells you which local branches are linked to a remote one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git branch -vv
  bugfix/header   aba805a My change
  feature/login   796f733 [origin/feature/login] Add login
  feature/profile aba805a My change
  feature/search  aba805a [origin/feature/search] My change
  hotfix/typo     aba805a My change
* main            aba805a [origin/main] My change
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Branches with a name in square brackets are tracking a remote branch. &lt;code&gt;bugfix/header&lt;/code&gt; has none, which means it only exists on your computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a branch from another branch or an old commit
&lt;/h2&gt;

&lt;p&gt;By default a new branch starts from wherever you are. Add a starting point at the end to begin somewhere else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git switch -c hotfix/typo aba805a
Switched to a new branch &lt;span class="s1"&gt;'hotfix/typo'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The starting point can be a commit hash (find one with &lt;code&gt;git log --oneline&lt;/code&gt;), another branch name such as &lt;code&gt;main&lt;/code&gt;, or a tag like &lt;code&gt;v1.2.0&lt;/code&gt;. A common pattern is starting a fix from the released version: &lt;code&gt;git switch -c hotfix/login v1.2.0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a local branch from a remote branch
&lt;/h2&gt;

&lt;p&gt;When a teammate has pushed a branch you want to work on, fetch it, then switch to it by name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git fetch
$ git branch -r
  origin/HEAD -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; origin/main
  origin/feature/login
  origin/main
$ git switch feature/login
Switched to a new branch &lt;span class="s1"&gt;'feature/login'&lt;/span&gt;
branch &lt;span class="s1"&gt;'feature/login'&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; up to track &lt;span class="s1"&gt;'origin/feature/login'&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git spots that &lt;code&gt;feature/login&lt;/code&gt; exists on &lt;code&gt;origin&lt;/code&gt;, creates a local branch with the same name and links the two, so &lt;code&gt;git pull&lt;/code&gt; and &lt;code&gt;git push&lt;/code&gt; work straight away. If you want a different local name, spell it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git switch -c feature/payments origin/feature/login
Switched to a new branch &lt;span class="s1"&gt;'feature/payments'&lt;/span&gt;
branch &lt;span class="s1"&gt;'feature/payments'&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; up to track &lt;span class="s1"&gt;'origin/feature/login'&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Push a new branch to GitHub
&lt;/h2&gt;

&lt;p&gt;A branch you create locally stays on your computer until you push it. The first push needs &lt;code&gt;-u&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push -u origin feature/search
To https://github.com/you/my-app.git
 * [new branch]      feature/search -&amp;gt; feature/search
branch 'feature/search' set up to track 'origin/feature/search'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, a plain &lt;code&gt;git push&lt;/code&gt; is enough. If you forget &lt;code&gt;-u&lt;/code&gt;, Git tells you the branch "has no upstream branch" and prints the exact command to run. Our step-by-step guide to &lt;a href="https://gitcommands.si/git-push-to-github/" rel="noopener noreferrer"&gt;pushing code to GitHub&lt;/a&gt; covers login problems and rejected pushes too.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to name a branch
&lt;/h2&gt;

&lt;p&gt;Git is strict about a few characters. A branch name can't contain spaces, &lt;code&gt;..&lt;/code&gt;, &lt;code&gt;~&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, &lt;code&gt;:&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt; or &lt;code&gt;[&lt;/code&gt;, can't start with a dash, and can't end with a slash or &lt;code&gt;.lock&lt;/code&gt;. Break a rule and you'll see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ git branch &lt;span class="s2"&gt;"bad name"&lt;/span&gt;
fatal: &lt;span class="s1"&gt;'bad name'&lt;/span&gt; is not a valid branch name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beyond the rules, most teams use a &lt;code&gt;type/short-description&lt;/code&gt; pattern, which also groups branches neatly in tools like GitHub:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;feature/user-signup&lt;/code&gt; for new work&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bugfix/cart-total&lt;/code&gt; for fixes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;hotfix/login-crash&lt;/code&gt; for urgent fixes to a release&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs/readme-install&lt;/code&gt; for documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep names short, lowercase and hyphenated. Adding a ticket number, like &lt;code&gt;feature/142-user-signup&lt;/code&gt;, makes it easy to trace a branch back to its task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors you might see
&lt;/h2&gt;

&lt;h3&gt;
  
  
  "fatal: a branch named 'x' already exists"
&lt;/h3&gt;

&lt;p&gt;You already have a branch with that name. Switch to it with &lt;code&gt;git switch x&lt;/code&gt;, pick another name, or rename the old one. Our guide shows how to &lt;a href="https://gitcommands.si/git-rename-branch/" rel="noopener noreferrer"&gt;rename a Git branch&lt;/a&gt; locally and on GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  "fatal: invalid reference: x"
&lt;/h3&gt;

&lt;p&gt;Git can't find the branch you're switching to. Check the spelling with &lt;code&gt;git branch -a&lt;/code&gt;. If it only exists on the remote and doesn't show up, run &lt;code&gt;git fetch&lt;/code&gt; and try again. To create it instead of switching to it, add &lt;code&gt;-c&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  "You are in 'detached HEAD' state"
&lt;/h3&gt;

&lt;p&gt;You checked out a commit directly instead of a branch. You can look around safely, but new commits won't belong to any branch. Run &lt;code&gt;git switch -c new-branch-name&lt;/code&gt; to keep them on a proper branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  When you're done with the branch
&lt;/h2&gt;

&lt;p&gt;Every time you create a new branch, plan how it will end. Once your work is finished, switch back to &lt;code&gt;main&lt;/code&gt; and merge it in with &lt;code&gt;git merge feature/search&lt;/code&gt;, or open a pull request on GitHub. If you made several messy commits along the way, you can &lt;a href="https://gitcommands.si/git-squash-commits/" rel="noopener noreferrer"&gt;squash them into one&lt;/a&gt; first. Then clean up: &lt;code&gt;git branch -d feature/search&lt;/code&gt; deletes the branch locally, and our guide to &lt;a href="https://gitcommands.si/git-delete-branch/" rel="noopener noreferrer"&gt;deleting a Git branch&lt;/a&gt; covers removing it from GitHub too. For every other branch command, from &lt;code&gt;git branch --merged&lt;/code&gt; to &lt;code&gt;git rebase&lt;/code&gt;, see the &lt;a href="https://gitcommands.si/" rel="noopener noreferrer"&gt;full list of Git commands&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ: creating branches in Git
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the Git command to create a new branch?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;git switch -c &amp;lt;name&amp;gt;&lt;/code&gt; creates a new branch and switches to it. &lt;code&gt;git checkout -b &amp;lt;name&amp;gt;&lt;/code&gt; does the same on older Git versions. &lt;code&gt;git branch &amp;lt;name&amp;gt;&lt;/code&gt; creates the branch without switching.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between git switch -c and git checkout -b?
&lt;/h3&gt;

&lt;p&gt;None in the result: both create a branch and move you onto it. &lt;code&gt;git switch&lt;/code&gt; arrived in Git 2.23 as a clearer command that only deals with branches, while &lt;code&gt;git checkout&lt;/code&gt; also restores files.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I see all branches in Git?
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;git branch -a&lt;/code&gt; to list local and remote branches together. Use &lt;code&gt;git branch&lt;/code&gt; for local ones only and &lt;code&gt;git branch -r&lt;/code&gt; for remote ones only. Run &lt;code&gt;git fetch&lt;/code&gt; first so the remote list is current.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I switch to another branch?
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;git switch &amp;lt;branch-name&amp;gt;&lt;/code&gt;, or &lt;code&gt;git checkout &amp;lt;branch-name&amp;gt;&lt;/code&gt; on older Git. Commit or stash your changes first if Git says they would be overwritten.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does creating a branch copy my files?
&lt;/h3&gt;

&lt;p&gt;No. A Git branch is just a pointer to a commit, so creating one is instant and takes almost no space, no matter how big the project is.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I push a new local branch to GitHub?
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;git push -u origin &amp;lt;branch-name&amp;gt;&lt;/code&gt;. The &lt;code&gt;-u&lt;/code&gt; flag links your local branch to the one on GitHub, so later pushes and pulls need no extra arguments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://gitcommands.si/git-create-branch/" rel="noopener noreferrer"&gt;gitcommands.si&lt;/a&gt;, where you'll also find the &lt;a href="https://gitcommands.si/" rel="noopener noreferrer"&gt;full list of 233 Git commands&lt;/a&gt; with examples.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
