I have sold my house and I rented it out for 4 of the 12 years I lived there how do capital gains work - the house price significantly increased after I moved back in?

I cannot seem to find online how to fill in the appropriate forms - I do not want to pay more capital gains than required and should not have to pay for any of the years I have lived there but it is not clear