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.
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
master
branch.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
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. π