In such case (toggling certain RP features or editing the shader source), re-building the project should only compile the necessary shaders / variants. The Editor also compiles and caches shader variants that are needed for viewport rendering.
You can use the Editor log to track the variants actually being compiled during build:
Compiling shader "Universal Render Pipeline/Lit" pass "ForwardLit" (fp)
[2.09s] 100M / ~100M prepared
320 / 786432 variants left after stripping, processed in 6.77 seconds
starting compilation...
finished in 29.72 seconds. Local cache hits 202 (0.24s CPU time), remote cache hits 0 (0.00s CPU time), compiled 118 variants (582.41s CPU time), skipped 0 variants
Above log is for 2020.2+ , but prior to this version you can also enable Shader Variant Log level in the URP/HDRP asset settings.
We understand the long build times are a huge pain, and are working on an immediate optimization for reducing shader build times via variant keyword prefiltering. Please check out the official announcement for more detail on upcoming improvements, as well as useful information on shader variant logging and stripping: https://forum.unity.com/threads/improvements-to-shaders-build-time-and-runtime-memory-usage.1305615/