Git Branch Create Command Examples |Video upload date:  · Duration: PT5M38S  · Language: EN

Examples of creating Git branches with commands and best practices for local and remote branches

If your current branch strategy feels like a wild safari, welcome to the club. Branches are how you experiment, fix bugs, and avoid pushing half-baked features to main. This guide gives the straight facts and a little attitude while you learn how to create branches, start work, publish them, and clean up the mess when you're done.

Create and switch in one move

If you want to make a branch and start working without the extra steps try one of these shortcuts. Both create a branch and move HEAD so your working tree follows along.

git checkout -b feature-name
# or the newer command
git switch -c feature-name

Use git checkout -b if you love tradition. Use git switch -c if you prefer clearer intent and slightly less cognitive load. Both end with you on feature-name ready to type code and possibly regret.

Create a branch without switching

Want to record a branch but stay put in your current working tree? Fine. Create the branch reference and keep working where you are.

git branch feature-name

This only writes the branch pointer. HEAD stays where it is, so your files do not change. Great for preparing branches while your editor binge compiles in the background.

Create a branch from a specific commit or tag

Sometimes you need a branch that starts from a past commit or a release tag. Point the branch command at that reference and Git will obey.

git branch hotfix v1.2.0
# or from a commit hash
git branch bugfix abc1234

The new branch will start from the given tag or commit instead of current HEAD. This is how you make a hotfix without dragging in unrelated work.

Publish the branch and set upstream

Local branches are great until collaboration is required. Push the branch to the remote and tell Git where to track changes so future syncs are painless.

git push -u origin feature-name

The -u option sets the upstream so later git pull and git push know which remote branch to use. It saves you typing and prevents awkward guessing games.

Verify branches and clean up when finished

List branches and spot the stray ones that outlived their usefulness.

git branch --all
# show branches merged into current branch
git branch --merged
# delete a branch that has been merged
git branch -d old-branch
# force delete when you really mean it
git branch -D old-branch

Use git branch --all to see local and remote branches. Use git branch --merged to find branches safe to remove. Be cautious with -D because it will rip out branches even when they are not merged.

Quick checklist

  • Create and switch in one step with git checkout -b or git switch -c
  • Create a branch without switching with git branch name
  • Branch from a tag or commit by giving that reference after the name
  • Publish and set upstream with git push -u origin name
  • List and clean up using git branch --all and git branch -d

There you go. You now have the commands to create branches without starting a minor civil war in your repo. Use them wisely and remember that branches are cheap but merge conflicts are memorable.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.