I'm little confused about this. How to know should we use himself or by himself?
ex. Hi will do it himself/by himself.
I know this is a stupid example but this is first what I remembered. What are the differences between them?
One is "instead of anyone else"
The other is "all alone"
He did it himself - because he trusted no-one else to do it.
He did it by himself - because he couldn't find anyone else to assist.
"Himself" or "herself" stresses that a person would do something rather than anybody else.
He did not trust his son to drive the car so he drove it himself.
We would use "by himself" or "by herself" to stress that something was being done alone.
She found no-one to help her to paint the fence so she did it by herself.
I think the other answers are only partially correct.
First, it's not true that himself always means "instead of anyone else."
Second, while there are cases where himself and by himself can be used interchangeably and only have a difference in meaning, sometimes they cannot be interchanged at all because one simply can't be used.
It's acceptable (although a bit nonstandard) to say:
Other people said they liked to arm wrestle competitively, so he will do it himself too.
This does not mean that he wants to do it to the exclusion of other people. Many other people do it, and he wants to partake in the same activity.
On the other hand, it's not okay to say:
Other people said they liked to arm wrestle competitively, so he will do it by himself too.
Semantically, it's not possible to arm wrestle competitively without an opponent. (Please don't bring up arm wrestling a machine set to a certain calibration . . .)
This shows there are additional use cases that should also be considered.
I will note that by adding too to the phrase I may have brought up something specifically different. However, in conversation, it could be left out:
"What about competitive arm wrestling?"
"He will do it [himself / by himself]."