Skip to main content

Command Palette

Search for a command to run...

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.

Updated
β€’3 min read
GIT Master Branch History Lost! - A Quick Solution
T

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. 😭 HotMessGIF.gif

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. 😰

OmgOmgOmgOmgGIF.gif

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. AomSusharAomGIF.gif

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.

  1. Update the local master branch.
    git pull origin master
    
  2. Create a new local branch from the local master.
    git branch SomeInterestingName
    
  3. Do your changes in the new branch as needed for the task.
  4. Your changes are ready.
  5. Commit and push.
    git commit -m "Some message"
    git push origin SomeInterestingBranchName
    
  6. Merge master and create a PR.

    a. Now checkout master.

    git checkout master
    

    b. Update master.

    git pull origin master
    

    c. Now checkout your new branch.

    git checkout SomeInterestingBranchName
    

    d. Let’s merge master into your new branch.

    git merge master
    

    e. Commit and push.

    git commit -m "Some message"
    git push origin SomeInterestingBranchName
    

    f. 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. πŸ™‚