Reverting a Reverted Commit and Restoring Previous Changes in Git

On one of my live projects, I needed to work with JavaScript scripts related to Facebook Pixel, so I created a branch `add-fb-scripts`. After deploying to production, I discovered that the event ID was not passing correctly, causing events to not be sent to Facebook. 

I had to revert the pull request. When I started working again on the same branch, I found that Git was only showing the latest commit, not the previous ones. To resolve this issue, I followed these steps : 


On one of my live projects, I needed to work with JavaScript scripts related to Facebook Pixel, so I created a branch add-fb-scripts. After deploying to production, I discovered that the event ID was not passing correctly, causing events to not be sent to Facebook.

I had to revert the pull request. When I started working again on the same branch, I found that Git was only showing the latest commit, not the previous ones. To resolve this issue, I followed these steps :

Step 1: Checkout the Branch, Make Fixes, and Push Changes

First, I checked out my branch, made the necessary fixes, and committed the changes.

git checkout add-fb-scripts # replace 'add-fb-scripts' with you branch name
git commit -m "Event id issue fixed."

Step 2: Create a New Tracking Branch from Master

Next, I created a new tracking branch from the master branch.

git checkout -b fb-scripts-fixes -t master 
# you can give any name to branch, in my case it is 'fb-scripts-fixes'

Step 3: Revert the Reversion Commit

I searched for the revert commit hash using git log, copied the hash, and then reverted the commit using the -m option.

git log | grep revert -A 5 -B 5  # Find the revert commit hash
# Example commit hash : c55baee4f9449b7e...

when i found required revert commit i executed this command

git revert c55baee4f9449b7e...  # Attempt to revert the commit

When I ran this command, Git gave me a fatal error:

Fatal error: commit c55baee4f9449b7e... is a merge but no -m option was given.

This means Git requires a commit message, so I did this:

git revert -m 1 c55baee4f9449b7e...  

# Revert the commit with the -m option
# Resolve any conflicts and continued revert

Step 4: Checkout the Initial Branch

I then checked out the initial branch again.

git checkout add-fb-scripts

Step 5: Merge the New Branch Where the Revert Was Applied

Finally, I merged the fb-scripts-fixes branch to include both the new fixes and the previous changes.

git merge fb-scripts-fixes

By following these steps, I now had all the changes on my branch, including the new commits as well as the previous ones.

I found this solution on Stack Overflow