Trying to rotate the object so it would always face the other object's normal

Hi everybody!
I’m trying to write a script where the object will rotate depending on another object that is being found by raycasthit, here’s a script:

{
    Ray ray = new Ray(transform.position, transform.forward);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit))
    {
        Vector3 normal = hit.normal;
        Debug.Log("The normal of the surface hit is: " + normal);
        Debug.Log("Hit object: " + hit.collider.gameObject.name);
        transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
    }
    Debug.DrawRay(ray.origin, ray.direction, Color.green);
}

But there’s a problem. In the first frame where object’s raycast touches another object, object’s rotation changes to 90, 0, 0. Obviously it shouldn’t work like that and after that raycast can’t scan any other objects because it’s directed in another way. Can you help me fix that?
Here’s how it looks:

The end result should be small cube rotating with big cube and always facing the normal that collides with small cube’s raycast.
I’ve tried the transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; from other thread which had similar problem , but it didnt help. Appreciate if somebody helps

FromToRotation creates a quaternion that that takes you from orientation following the vector from to an orientation following the vector to.
So what’s kinda rotation makes you go from looking up to looking forward ? it’s a 90 on x-axis.
What you want to rotate is the object’s transform.forward and NOT transform.up.
Also , this whole thing can be simpler if you just do this and call it day.

 transform.rotation = Quaternion.LookDirection(hit.normal , Vector3.up);

Edit : maybe i didn’t understand what you want from your title , if you mean the other objects “forward” when you say “normal” , then maybe change it to this

 transform.rotation = Quaternion.LookDirection(hit.transfrom.forward, Vector3.up);

@bloodthirst69 your piece of code changed something but there’s still a problem. Now after hitting raycasthit it rotates in right axis, but still not as intended:

The code looks like this now. Maybe i didnt set up something right?

void Update()
{
    Ray ray = new Ray(transform.position, transform.forward);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit))
    {
        Vector3 normal = hit.normal;
        Debug.Log("The normal of the surface hit is: " + normal);
        Debug.Log("Hit object: " + hit.collider.gameObject.name);
        transform.rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
    }
    Debug.DrawRay(ray.origin, ray.direction, Color.green);
}