Search

Gihub branches

Git branch


How to Safely Create, Use, and Delete Branches in GitHub

When working on a GitHub project, it’s important to maintain a clean and stable live environment while continuing to develop new features or fix bugs. One of the best ways to do this is by creating and using branches. This allows you to make changes without affecting the live version of your site or application.

In this guide, I’ll show you how to create a branch, work with it, and safely delete it after you’re done. Let’s dive in!

1. Cloning Your GitHub Repository

If you haven’t already cloned your repository to your local machine, you’ll need to do that first. Run the following commands in your terminal:

git clone https://github.com/your-username/your-repository.git
cd your-repository

This will clone the repository to your computer and change the directory to the repository folder.

2. Creating a New Branch

Now, let’s create a new branch. This branch will be where you make your changes while keeping the main branch (usually called main or master) unchanged.

git checkout -b feature-branch-name

Replace feature-branch-name with something descriptive like new-design, bugfix, or whatever makes sense for the changes you’re making.

3. Making Changes

Once you’ve created and switched to your new branch, go ahead and make the changes you want. You can always check which branch you’re on by typing:

git branch

This will show a list of branches and highlight the one you’re currently on.

4. Staging, Committing, and Pushing Your Changes

After making your changes, the next step is to stage and commit them. This saves your changes to the branch and prepares them to be pushed to GitHub.

git add .
git commit -m "Your commit message describing the changes"

Once you’ve committed the changes, push the branch to GitHub:

git push origin feature-branch-name

5. Creating a Pull Request (Optional)

If your changes are ready to be merged back into the main branch, you can create a Pull Request (PR). This allows you (or your team) to review the changes before they go live.

  1. Go to your repository on GitHub.
  2. Click on the Pull Requests tab.
  3. Click New Pull Request.
  4. Choose the main branch as the base branch and your feature-branch-name as the compare branch.
  5. Click Create Pull Request.

This will allow others to review and approve your changes before merging them into the live branch.

6. Deploying the New Branch (Optional)

If you have a staging environment, it’s a good idea to deploy your new branch there first. This way, you can test the changes in a live-like environment without affecting the actual live site.

7. Merging the Branch to the Live Site

Once you’re confident your changes are solid and thoroughly tested, it’s time to merge them into the live branch (usually main or master).

  1. First, switch back to the main branch:

    git checkout main
  2. Make sure the local main branch is up to date:

    git pull origin main
  3. Merge your feature branch into the main branch:

    git merge feature-branch-name
  4. Finally, push the updated main branch to GitHub:

    git push origin main

Your changes should now be live!

8. Deleting the Branch

Once your feature has been merged into the main branch, it’s a good idea to clean up by deleting the branch. This helps keep your repository tidy.

Deleting a Local Branch

To delete the branch locally:

git branch -d feature-branch-name
  • The -d flag checks if the branch has been merged before deleting it.

  • If the branch hasn’t been merged and you still want to delete it, use -D:

    git branch -D feature-branch-name

Deleting a Branch on GitHub (Remote)

To delete the branch from GitHub (remote repository), use:

git push origin --delete feature-branch-name

This will remove the branch from the GitHub repository.

Conclusion

By following these steps, you can safely develop new features, fix bugs, and maintain a clean and stable live environment. Working with branches in GitHub allows for flexibility, organization, and control, ensuring your project continues to grow without risking stability.

Happy coding!