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.
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.
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.
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.
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
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
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.