GIT Master Branch History Lost! - A Quick Solution
In this blog, we will see how to come out of a situation when your master branch becomes corrupted.

As a π Vice President with over 12 years of experience, I am a seasoned software architect known for designing and leading teams of engineers to deliver high-quality software products promptly and within budget.
Throughout my career, I have spearheaded the end-to-end design of 7 innovative software products π―.
From conceptualization to deployment planning, I have successfully guided teams through requirements gathering, prototyping, testing, and deployment phases, ensuring exceptional outcomes.
I take pride in my industry recognition, including being honored as a π Microsoft Most Valuable Professional, π‘ CodeProject Most Valuable Professional, and π DZone Most Valuable Blogger.
Additionally, my expertise has been acknowledged by BookAuthority, which recognized my books on ASP.NET, REST API, Vue.js, and Dependency Injection as the π best of all time.
In addition to my professional achievements, I am passionate about mentorship and have been privileged to serve as a π Young Mentor at IndiaMentor, guiding aspiring professionals in their career journeys.
For further information about my work and insights, please visit my website at π http://taditdash.com.
You can also connect with me on π¦ Twitter at https://twitter.com/taditdash, π Facebook at https://www.facebook.com/taditdash, and πΌ LinkedIn at https://www.linkedin.com/in/taditdash.
I am always open to networking and discussing opportunities, so feel free to reach out and connect.
Let's explore how we can collaborate and drive innovation in the ever-evolving world of software architecture and development.
Background
While working on a past project, we encountered a problem while pushing the local changes from a new computer that has an old version of the master branch. We did not realize it was old initially though. π
Issue while merging
The error was not so clear and it was just saying push failed. Thinking that it might be an access issue, we then tried to give that user the required access by changing the role to maintainer at GitLab repository members settings. Nothing helped. π

Then we messed up the master branch by forcing a push from that computer. ππ
git push --force
As a result, the master branch got replaced with the old version, and all its history got removed. π°

What to do now?
The team was in shock. No one has a clue. After a round of research, we came to a conclusion. To restore everything to normal, we then followed the steps mentioned below. The idea is to make the master point to an old commit that can be considered as most recent. Which means we need to find it somewhere. π¬
Find out a recent master commit
From another computer of a teammate, that had the latest master branch, we pointed the head to a previous commit.
git reset <commit hash>
NOTE: Commit hash can be fetched by looking into the git log.
Force push to remote master
Then pushed that local master to the remote master by force again.
git push --force
Now, the remote master pointed to a commit that was not so old and solved the purpose.

Learning
Due to all this, we learned a process that we need to follow every time we merge something to the remote master. π
Letβs analyze that below. β
Basic Process of New Branch Commit, Push, and Merge
Here are some basic steps we followed in the team to prevent this issue in the future. Whenever you are ready with a change that can be merged to master, follow these steps.
- Update the local
masterbranch.git pull origin master - Create a new local branch from the local
master.git branch SomeInterestingName - Do your changes in the new branch as needed for the task.
- Your changes are ready.
- Commit and push.
git commit -m "Some message" git push origin SomeInterestingBranchName Merge
masterand create a PR.a. Now checkout
master.git checkout masterb. Update
master.git pull origin masterc. Now checkout your new branch.
git checkout SomeInterestingBranchNamed. Letβs merge
masterinto your new branch.git merge mastere. Commit and push.
git commit -m "Some message" git push origin SomeInterestingBranchNamef. Your branch and master are now in sync.
g. Now go to your SCM like GitLab/GitHub and select your branch.
h. Create a Pull Request with the target branch as
master.i. After PR is approved, merge after resolving the conflicts, if any. β
Would like to hear from you?
Have you faced such a situation? How did you solve it? It would be very interesting to discuss. Put a comment below. π
Thanks for reading. Have a nice day. π




