Creating a New Local Branch with Git

Photo by Annie Spratt on Unsplash

Have you been adding new features to your project directly onto your main branch? If you have, it’s time to start creating new branches for different features!

What are some good reasons to create new branches? Well, you can work on different features simultaneously without worrying about your base code, it’s a lot cleaner, and it’s easier to manage!

To get started with creating a new branch from git, simply go into your project directory and type into the terminal: git branch <new branch name>. You can now switch between branches by using the git checkout <desired branch name> command.

After switching to your new branch and committing your changes, push to your remote repository as normal! This should automatically create the new branch remotely.

For me, I prefer to make my pull requests straight from Github. Once you do this, you can checkout your main or master branch and git pull. After resolving any conflicts, you can now safely delete your branch. Make sure you’re not on the branch you’re trying to delete, or Git will not let you delete it.

git checkout main then git branch -d <branch to delete>. The lowercase -d will delete the branch if changes have been merged and pushed to a remote repository. If you’d like to delete the branch without pushing your code remotely, use a capital -D instead of lowercase.

To delete the remote branch, use the command git push <remote> --delete <branch>. For example, git push origin --delete authentication.

Once that’s done, be sure to synchronize, or “prune,” your local repository with your remote repository by executing git fetch -p.

That’s it! Happy branching.