Back to Git & Dev Tools
2026-03-207 min read

Delete a branch and pull main into local working branch (Git & Dev Tools)

Learn Delete a branch and pull main into local working branch (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In software development, managing code effectively using version control systems like Git is crucial. One of the fundamental tasks is branching—creating separate lines of development for new features or bug fixes without affecting the main codebase. However, merging these branches back into the main branch (usually main or master) is equally important to keep your project up-to-date and avoid conflicts. This lesson will guide you through the process of deleting a branch and pulling the main branch into your local working branch, providing you with a solid understanding of Git workflows.

Prerequisites

To follow this tutorial, you should have basic knowledge of Git and command line navigation. Familiarity with creating branches and committing changes is assumed. If you're new to Git, consider checking out our Git Basics lesson first.

It's also essential to understand the concept of remote repositories and how to configure them. You can learn more about that in our Git Remote Repositories lesson.

Core Concept

Branching in Git

In Git, branches represent separate lines of development for your project. You can create a new branch to work on a feature or bug fix without affecting the main codebase. Once you're done with your changes, you need to merge the branch back into the main branch.

Deleting a Branch

To delete a Git branch locally, use the git branch -d command followed by the name of the branch you want to delete. For example:

git branch -d my-feature-branch

If you try to delete a branch that has unmerged changes, Git will prevent you from doing so and ask you to either switch to that branch or use -f (force) option. It's generally recommended to resolve any conflicts before deleting the branch.

Pulling Main into Local Working Branch

To pull changes from the main branch into your local working branch, first ensure you have switched to the appropriate branch using git checkout. Then, fetch and merge the main branch with your current branch:

git checkout my-working-branch
git fetch origin
git merge origin/main

This will merge the changes from the main branch into your local working branch. If there are any conflicts, Git will prompt you to resolve them before completing the merge.

Remote Branches and Deletion

To delete a remote branch using Git, you can use git push origin --delete . This command deletes the specified branch from the remote repository. Be cautious when deleting remote branches as they cannot be recovered once deleted.

Worked Example

Let's walk through an example where we have a project with two branches: main and my-feature-branch. We've made some changes in my-feature-branch, and now we want to delete it and pull the latest changes from main into our local working branch (my-working-branch).

  1. First, ensure you have switched to your local working branch:
git checkout my-working-branch
  1. Fetch the latest changes from the remote repository:
git fetch origin
  1. Now, delete the my-feature-branch locally:
git branch -d my-feature-branch
  1. To ensure that there are no unmerged changes in the remote my-feature-branch, merge it with the local main branch:
git checkout main
git merge origin/my-feature-branch
  1. If there are any conflicts, resolve them and commit the merge. Once resolved, switch back to your local working branch:
git checkout my-working-branch
  1. Finally, pull the latest changes from the main branch into your local working branch:
git fetch origin
git merge origin/main
  1. To delete the my-feature-branch remotely (assuming you have push access), use:
git push origin --delete my-feature-branch

Now you have successfully deleted a branch and pulled the latest changes from the main branch into your local working branch, as well as removed the branch from the remote repository.

Common Mistakes

  1. Trying to delete a branch with unmerged changes: Git will prevent you from deleting the branch if there are unmerged changes. Make sure to resolve any conflicts before trying to delete the branch.
  1. Deleting the wrong branch: Double-check the name of the branch you're trying to delete, especially if you have multiple branches with similar names.
  1. Force deleting a protected branch: If your branch is protected in GitHub or another hosting service, using git push -f may fail. In this case, unprotect the branch, delete it locally, and then push it back to the remote repository.
  1. Merging conflicts during the merge process: Git will prompt you to resolve any conflicts that arise during the merge process. Make sure to address them before completing the merge.
  1. Deleting a branch that others are still working on: Be cautious when deleting branches, as it may affect other team members who are still using or dependent on those branches. Always coordinate with your team before deleting shared branches.
  1. Not updating remote branches after resolving conflicts locally: After resolving conflicts and committing the changes in your local working branch, make sure to push those updates to the remote repository using git push.

Practice Questions

  1. You have a feature branch named my-feature with unmerged changes. How can you delete it?
  2. You've created a new branch, but you forgot to switch to it before making changes. How can you move your changes into the new branch?
  3. Your teammate has pushed changes to the main branch, and you want to pull those changes into your local working branch. What command should you use?
  4. You have a branch named my-branch that you want to delete, but it's protected in GitHub. How can you delete it?
  5. You're trying to merge the main branch into your local working branch, but there are conflicts. How do you resolve them and complete the merge?
  6. A team member has created a new feature branch named my-feature, and they have forgotten to push their changes to the remote repository. What steps should you take to ensure that their changes are included in the main branch?
  7. You're working on a feature branch, but you realize that the main branch needs an urgent fix. How can you safely merge the changes from the main branch into your feature branch without affecting your work?
  8. Your team has agreed to delete a shared branch named my-shared-branch. However, one of your team members is still working on it. What should you do before deleting the branch?
  9. You have a local repository with multiple branches, but you want to keep only the main and develop branches. How can you delete all other branches locally?
  10. You've made some changes in your local working branch, and you want to push those changes to a new branch on the remote repository. What steps should you follow?

FAQ

Q: Can I delete a remote branch using Git?

A: Yes, you can delete a remote branch using git push origin --delete .

Q: What happens if I force-delete a branch with unmerged changes?

A: If you force-delete a branch with unmerged changes, Git will discard the local changes in that branch. Be cautious when using the -f option.

Q: How can I merge my feature branch back into the main branch if there are conflicts?

A: Resolve the conflicts by editing the conflicting files, then add and commit the resolved changes. After resolving conflicts, use git merge --continue to complete the merge.

Q: Can I delete a branch that has been merged into the main branch?

A: Yes, you can safely delete a branch after it has been merged into the main branch as long as there are no unmerged changes in the branch.

Q: How do I rename a Git branch locally and remotely?

A: To rename a local branch, use git branch -m . To rename a remote branch, first delete the old remote branch using git push origin --delete , then create a new remote branch with the desired name: git push origin .

Q: How can I view the history of a Git branch?

A: To view the commit history for a specific branch, use git log --oneline . This command displays the commit history in a concise format with each commit's hash and message.

Delete a branch and pull main into local working branch (Git & Dev Tools) | Git & Dev Tools | XQA Learn