How to Create a New Branch in GitHub Command Line
Creating a new branch in GitHub is an essential skill for any developer. Whether you are working on a feature, fixing a bug, or preparing for a release, branches allow you to isolate your changes from the main codebase. In this article, we will guide you through the process of creating a new branch in GitHub using the command line.
Step 1: Open Your Terminal or Command Prompt
Before you start, make sure you have Git installed on your computer. To check if Git is installed, open your terminal or command prompt and type `git –version`. If you see a version number, Git is installed.
Step 2: Navigate to Your Repository
Next, navigate to the directory of your GitHub repository using the `cd` command. For example, if your repository is located in a folder named “my-repo” on your desktop, you would type:
“`
cd Desktop/my-repo
“`
Step 3: Create a New Branch
To create a new branch, use the `git checkout -b` command followed by the name of your new branch. For instance, if you want to create a branch named “feature/new-feature”, you would type:
“`
git checkout -b feature/new-feature
“`
This command creates a new branch named “feature/new-feature” and switches to it simultaneously.
Step 4: Verify the New Branch
After creating the new branch, you can verify its existence by running the `git branch` command. This command lists all the branches in your repository, including the newly created branch. You should see “feature/new-feature” listed among the branches.
Step 5: Start Working on Your New Branch
Now that you have created a new branch, you can start making changes to your code. Make sure to commit your changes regularly using the `git commit` command. This will help you keep track of your progress and facilitate collaboration with other team members.
Step 6: Push Your New Branch to GitHub
Once you have finished working on your new branch, you can push it to GitHub using the `git push` command. This will create a copy of your branch on the remote repository. To push your branch, type:
“`
git push origin feature/new-feature
“`
Replace “origin” with the name of your remote repository if it’s different.
Step 7: Merge or Delete Your New Branch
After you have completed your work on the new branch, you can merge it with the main branch or delete it, depending on your requirements. To merge the branch, use the `git merge` command followed by the name of the branch you want to merge. For example:
“`
git merge feature/new-feature
“`
To delete the branch, use the `git branch -d` command followed by the name of the branch. For example:
“`
git branch -d feature/new-feature
“`
Remember to push the changes to the remote repository before deleting the branch.
Conclusion
Creating a new branch in GitHub using the command line is a straightforward process. By following the steps outlined in this article, you can easily create, work on, and manage branches in your GitHub repository. Happy coding!