How to create a new branch on your forked GitHub repo via the command line – 041

Suppose you’ve forked a repo and have cloned a local copy. You’re ready to start making changes, but wait! first you’ll want to create a new branch first before making your modifications. (Don’t worry if you forget this step. I always do. Fortunately, you’re using Git now and there’s a way to do anything and everything it seems. I’ll cover how to get out of this state gracefully in a future tip.)

You could use two separate commands

> git branch <branch-name>

> git checkout <branch-name>

or use a shortcut that automatically creates a branch and switches to it using the –b switch:

> git checkout –b <branch-name>

as shown in the image below

git command to create and switch to a new branch

Suppose you’ve made a typo and misspelled windows as widnows.

You can fix this by renaming the current local branch using the following command:

> git branch –m windows

As seen in the image below.

renaming misspelled branch

Now you might be wondering why a –m switch and not a –rn switch. This is because –m stands for move, and since Git started on UNIX-based systems, Git uses a lot of the UNIX nomenclature. So to rename a file in a UNIX system, you need to move the file to its new name. Thanks to this SO answer.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s