I was facing this error - cannot lock ref "refs/heads/***/***", while doing git push command. In this article, we will see how we can get the best solution of this error. I was doing code and wanted to commit and push my codes, I tried to push my codes but I was getting this amezing error. This error can also come at the time of doing git push. I explored a lot then got to know the solution and wanted to share this solution here.
Why this error cannot lock ref "refs/heads/***/***"?
This error occures because it might possible that there is a branch with the same name already exist, or there is a
branch is already
created with the same name pattern.
For example, if you created a branch with ABC-1001 and later you want to create a
branch with name ABC-1001/xyz, then this will throw the below error:
Git Error: error: cannot lock ref "refs/heads/ABC-1001/xyz": "refs/heads/ABC-1001 exists; cannot create "refs/heads/ABC-1001/xyz"
What is the solution of this error - cannot lock ref "refs/heads/***/***" ?if you get the error: cannot lock ref "refs/heads/ABC-1001/xyz": "refs/heads/ABC-1001 exists; cannot create "refs/heads/ABC-1001/xyz", then you need to do the following things: 1. Delete the remote branch ABC-1001 2. Delete the local branch ABC-1001
1. Delete the remote branch ABC-1001We need to delete the remote branch unsing following git command:
Before doing this, you need to check the remote name like wether this would be origin or upstream or anything else that you added before. To check this, run following command:
And see the which remote location we need to delete the branch.
Git Error: error: cannot lock ref refs/heads/***/***: refs/heads/*** exists; cannot create refs/heads/***/***
Here I need to work on upstream, so my command would be -
Now the above command will delete the branch on your remote location.
You can also use this shorter command to delete a branch remotely:
2. Delete the local branch ABC-1001First of all, you need to checkout another branch which you are not going to delete, because git will not allow you to delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting.
git checkout master Now we need to delete the local branch using following command:
The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.
Now the local branch will be deleted successfully.
Now you have deleted the local and remote branches and you can create the branch and do the operation accordingly.
Strongly Recommended Books For You:
For Other Countries (United States, United Kingdom, Spain, Italy, Germany, France, Canada, etc)
If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.