I have a project that I worked alot on it, and now I'm thinking of asking people to contribute to it, but my concern is that people will take my existing code and do whatever they want with it(modify it and sell it etc) instead of contributing. What can I do to prevent it?
Any free and open source license will permit resale and modification.
However, you may be interested in a copyleft license like the GNU GPL, which requires binary distributions (modified or unmodified) to be accompanied by the corresponding source code under the same license terms. In this regard, any modification (that gets publicly distributed) is effectively similar to a contribution back to your project, because you can always pull other people's downstream changes back into your project.
Of course, there is a social aspect that you simply can't control: the ability to pull downstream modifications back into your project is quite different from the social power to control the other project's development direction, etc. There is always a possibility that you can lose developer interest by an influential person making a "hard fork" of your project and continuing new development without your involvement.
do whatever they want with it
From my perspective this is great. If they are able to build something with foreign code this means that the code has some quality. I think, if their own use case is followed by a modification, the question to me is how to make the contribution step.
- Mentoring, documentation, communication channels. When people know you, they like you or drop the project. If they are in touch with you they are more likely to succeed in a modification and as such to contribute. Community.
- Using a commonly known way to contribute - like Git, GitHub/GitLab, ... . Plus: You can see your forks and pull in the code.
- Giving up control. Invite people to commit to your project. You can always revert and throw them out. It also involves non-technical communication. It is a mental step for you to say what "my code" means and let go. Clear governance processes like CCCC help to communicate the rights of people to the code base. When the code is theirs as much as yours, joining forces is the easier option, if mutual trust exists.
instead of contributing
There is a difference between contributing and giving you the code. If they modify your code and pass that on, this might already be a contribution. It is not a feed-back contribution into the code you control but improves other people's lives.
Your question was how to prevent something. Trying my thinking in a non-violent way is: How to nourish what I would like to see? So, I did not really answer your question of how to prevent people from doing something which they may have good reason to do but how to enable them to do the things I would like to see them doing: Contribute. If you do not blame them for not contributing back but find the reasons why they fail to do so, there might be more chance to see them contribute. (Sorry for the unasked advice.)