Create a Git Branch From Another Branch |Video upload date:  · Duration: PT3M36S  · Language: EN

Learn how to create a new Git branch from a different branch with simple commands for local and remote workflows and common gotchas.

Why start a branch from another branch

So you want a branch that borrows history from some other branch and behaves like it always belonged to that lineage. Good idea. This is a standard git branching task that keeps your work based on the exact commit you want. It is essential for clean feature work in version control and for a sane developer workflow.

Refresh remote refs

Before you try anything clever, make sure your local git knows what the remote thinks. Otherwise you will create a branch from an outdated commit and then explain yourself to the team.

git fetch origin

This updates remote refs without touching your working tree. It is safe and boring in the best possible way.

Switch to the base branch

Pick the branch you want to base your new branch on and switch to it. You can use the classic command or the newer, slightly more polite command.

git checkout other-branch
# or on newer Git versions
git switch other-branch

Confirm the base branch contains the commit you want to inherit. If it does not, either wait or communicate. Merge conflicts do not respect schedules.

Create the new branch from the chosen base

Now create a branch that points at that base. You can create and check it out in one go. Pick whichever command makes you feel younger or more modern.

git checkout -b new-branch other-branch
# or
git switch -c new-branch other-branch
# if the base is only on the remote
git checkout -b new-branch origin/other-branch

At this moment the new branch points to the tip of the chosen base. Congratulations, you have a child branch. Treat it nicely.

Push the new branch to the remote

Share the branch so collaborators can see your masterpiece or your mess. The upstream flag makes future life easier.

git push -u origin new-branch
# after the first push you can just run
git push

Verify history and tidy up

Look at the history to make sure you actually started where you thought you did. The graph view is compact and honest.

git log --oneline --graph --decorate new-branch

If the base has moved on since you started then rebase the feature branch onto the updated base. This keeps history linear and slightly less chaotic.

git rebase other-branch

Quick workflow checklist

  • git fetch origin to update remote refs
  • git checkout other-branch or git switch other-branch to move to the base
  • git checkout -b new-branch other-branch or git switch -c new-branch other-branch to create and check out
  • git push -u origin new-branch to publish
  • git log --oneline --graph --decorate new-branch to inspect

Final tips for developers

If you need to delete a local branch after it is merged use git branch -d branch-name. If you need to remove a remote branch use git push origin --delete branch-name. Keep your branch names short and meaningful and write commit messages like you expect someone else to read them. Odds are they will.

This short git tutorial covers the practical git-commands you need for creating a branch from another branch in a reliable workflow. Follow these steps and you will avoid most awkward PR explanations.

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.