For quite some time, I’ve been building my own personal cheat sheet for Git and GitHub. It’s a quite handy resource, especially for those somewhat new to version control because there is a first-time-quickstart AND a second-time-quickstart that just has the steps one typically needs after the initial setup.
Another important tip and FAQ is.. “How do I create a new local branch from a remote branch that already exists?”
From the notes that were already provided, one might be tempted to think to just run “git checkout -b branch-name” then “git pull origin branch-name”, but that doesn’t always provide the expected results of the exact code that is on the remote branch. (So, these commands weren’t properly formatted for that reason, for those that like to skim fast and use whatever commands). Running those two commands mentioned in the previous sentence will create a new branch that already contains code from your currently active branch, which may not agree with the code that is in the new remote branch that you want. So, what to do?
First, here’s the steps in plain-ish English:
- Track the interested remote branch.
- Create a new branch based off that tracked remote branch.
Seems easy, right?
So, here’s the quick code for that, with comments explaining a bit for each line:
// Assuming `interesting-branch` is the remote branch that you want local. git fetch origin interesting-branch // Optional step to see that it is tracking, but not a real local branch yet: git branch -a // Create the local branch from the tracking branch: git checkout --track -b interesting-branch origin/interesting-branch
Now, treat you new “interesting-branch” as a regular local branch. Done!Now a quick word from a tech company who helps support me and this site: