Git Branching & Checking out

What is branching?

Branching mean we create a new line of commit history in a temporary working space. We do this to implement a feature to fix a big, and we give it branch name that describes the feature we are building or the bug we are fixing.

You create a new branch by using the -b flag on the git checkout command, like so:

What is checking-out?

Checking out means we are switching into a different branch. This will change all of the code you see in your code editor to match what’s in the branch you switched into.

We can only do this when our Git is in a clean state or when switching doesn’t cause a conflict.

What is a local branch vs. a remote branch?

Does branching on my machine (local) match what’s on Github?

Yes and no. Git does something called remote tracking which means you will have a copy of the branch locally and also push this copy to Github or Gitlab as you make commits.

You do this by pulling and pushing. Pulling means you are taking the remote commits and adding them onto the stack of commits you have on your corresponding local branch. Pushing means you take the commits you’ve made locally and send them to the repository on Github or Gitlab.

There are two ways to make your local branch “tracked” to one you will see on Github.

  1. When creating a new branch (git checkout -b), use the --track flag.
  2. If you forget to do this, Git will tell you it can’t push, like so:

Conveniently, Git gives you exactly the command to use to tell this local branch to push to a remote branch of the same name at the origin repository.

Remember, the keyword origin to represent your remote repository is the most common thing you’ll see to describe the repository that’s the main “source of truth” for your code — but it doesn’t have to be. It’s defined by your .git/config file and if you change it in your file. You typically don’t have more than one repository per project, but to push to Heroku or other Git-based deploy, you’ll need to set up a different repository. (See Pushing to Heroku)

Here, you can simply copy & paste the command Git gives you, then run it, which accomplishes three things:

  1. it will tell your local Git to track the local branch to the remote branch on origin/
  2. a new branch on Github will be created (automatically) and
  3. the current commits on this branch will be pushed up to Github

Leave a Reply

Your email address will not be published. Required fields are marked *