'Issue while merging two branches in Git

I have two branches master and Feature in Git. both branches are not in sync. I tried to merge these 2 branches but after merge I found many commits are missing. How to solve this issue. Below are the step which I performed

Checkout to master and did a pull 
perform git pull origin Feature from master branch
Resolve all conflicts and commit the changes
Push the code 


Solution 1:[1]

What you did is a wrong practice because you're pulling directly from feature to master assuming that after resolving conflicts you won't have any errors. So simply wrong. What you should've done is the following:

  1. You should go to your feature branch. (git checkout feature)
  2. Pull from master (git pull origin master)
  3. Resolve conflicts if any
  4. After resolving conflicts, test your code on feature branch.
  5. After testing your code and running your unit tests (if any), you merge to master. (git checkout master && git merge feature)

So find your old master head commit using git reflog, and do this: git reset --hard <commit> and do what I told you to do above.

If you want to validate the files changed you're merging to master, before performing step 5 do this:

git checkout feature
git diff master --name-status

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alan Deep