Strengthen and stretch your neck and upper back
Exercise has proven to be one of the best measures to prevent upper back and neck pain in the long term. When you exercise your back and neck muscles, they adopt an improved posture, and the strengthened muscles protect other structures from strain and damage.
There are multiple exercises and stretching techniques you can try. For example, the chin tuck exercise which simply consists of pulling your head straight back with your face looking forward. You can do it against the wall and merely align the back of the neck to the wall.
Strengthening exercises of your back are also beneficial in reducing neck strain. Studies show that this type of exercise may reduce the number and severity of neck pain episodes if you have a recurrent problem.