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.
Ensure you are on the
dev
branch: If you are not on thedev
branch, switch to it using the following command:git checkout dev
Merge the changes from
main
intodev
(if necessary): It's a good practice to merge the latest changes from themain
branch into thedev
branch to ensure you don't lose any important updates. If there are no changes inmain
that need to be merged, you can skip this step.git merge main
Delete the
main
branch: To delete themain
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
Rename the
dev
branch tomain
: To rename thedev
branch tomain
, use the following command:git branch -m dev main
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 oldmain
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.