Suppose, you have two git branches dev and main. You want to delete main branch and convert the dev branch into main branch. How can you do that?

Suppose, you have two git branches dev and main. You want to delete main branch and convert the dev branch into main branch. How can you do that?

Recently, I have faced an interesting problem. For a project, I had to delete my main branch and convert the dev branch into the main branch. Though it is possible, I have to be very careful because any mistake can destroy my whole project. Let's discuss, how I did it!

To delete the main branch and rename the dev branch to main in Git, you can follow these steps:

Please be careful when performing these operations, especially if you're working on a shared repository.

  1. Ensure you are on the dev branch: If you are not on the dev branch, switch to it using the following command:

     git checkout dev
    
  2. Merge the changes from main into dev (if necessary): It's a good practice to merge the latest changes from the main branch into the dev branch to ensure you don't lose any important updates. If there are no changes in main that need to be merged, you can skip this step.

     git merge main
    
  3. Delete the main branch: To delete the main branch, use the following command:

     git branch -d main
    

    If you get an error saying that the branch is not fully merged, use the following command to forcefully delete it:

     git branch -D main
    
  4. Rename the dev branch to main: To rename the dev branch to main, use the following command:

     git branch -m dev main
    
  5. Push the changes to the remote repository: After you've made these changes, you should push them to your remote repository so that others can see and use the updated branch names.

     git push origin main
    

    This will push your newly renamed main branch to the remote repository and delete the old main branch.

    If push doesn't work, you have to force push by using the below command

     git push -f origin main
    

Now, you've successfully deleted the main branch and converted the dev branch into the new main branch. Make sure to communicate these changes to your team if you're working in a collaborative environment, as it will affect the branch names that others are working with.

Hope you find this article helpful. If you have any suggestions, please write them in the comment section. Thanks.